zoukankan      html  css  js  c++  java
  • 🎪location对象

    window.location可以简写成location。location相当于浏览器地址栏,可以将url解析成独立的片段。

    一、属性

    1 console.log(location.host);//主机名,包括端口
    2 console(location.hostname);//主机名
    3 console.log(location.href); //完整的url(网址)
    4 console.log(location.pathname); //文件名(url中的部分路径)
    5 console.log(location.port); //端口号
    6 console.log(location.protocol); //协议(HTTP、https)
    7 console.log(location.search) //提交的text(查询字符串)

    二、location 每个查询字符串参数获取方法

    1、一个地址

    1 HTML、CSS、JS文件代码/BOM代码文件/04 location对象的常用属性.html?user=aaa&pwd=123
    2 
    3 //?user=aaa&pwd=123

    2、取得去掉问号的查询字符串

    1 var qs = location.search.length > 0? location.search.substring(1) : '';//user=aaa&pwd=123
    2 //从索引 1 开始取到后面的字符

    3、将取到的字符串且分开

    1 var items = qs.length? qs.split('&') : [];//['user=aaa','pwd=123']

    4、定义参数

    1 var item = null,//装 items 中的元素
    2 name = null,//装 item 中的名字
    3 value = null,//装名字对应的值
    4 args = {};//装结果

    5、循环取出 items 中的值进行操作

    1 for(i = 0;i<items.length;i++){
    2     item = items[i].split('=');//['name','aaa']等号分隔开
    3     name = decodeURIComponent(item[0]);
    4     value = decodeURIComponent(item[1]);
    5     if(name.length){
    6         args[name] = value;
    7     }
    8 }
    9 console.log(args);//示例:{user: "派大星", pwd: "cz"}

    6、用函数进行封装一下

     1 function userPwd(){
     2     //1、取得去掉问好的查询字符串
     3     var qs = location.search.length > 0? location.search.substring(1) : '';//user=aaa&pwd=123
     4     var items = qs.length? qs.split('&') : [];//['user=aaa','pwd=123']
     5     var item = null,name = null,value = null,args = {};
     6     for(i = 0;i<items.length;i++){
     7         item = items[i].split('=');//['name','aaa']
     8         name = decodeURIComponent(item[0]);
     9         value = decodeURIComponent(item[1]);
    10         if(name.length){
    11             args[name] = value;
    12         }
    13     }
    14     return args;
    15 }
    16 var newUserPwd = userPwd();
    17 console.log(newUserPwd);
  • 相关阅读:
    监控kubernetes集群的方式
    Prometheus的集群与高可用
    Grafana简单用法
    Prometheus实战之配置汇总
    Leetcode Surrounded Regions
    leetcode Spiral Matrix II
    leetcode Regular Expression Matching
    leetcode Set Matrix Zeroes
    leetcode 较难题II
    Leetcode 数独
  • 原文地址:https://www.cnblogs.com/songhaixing/p/12011416.html
Copyright © 2011-2022 走看看