🎪location对象
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(查询字符串)
1 HTML、CSS、JS文件代码/BOM代码文件/04 location对象的常用属性.html?user=aaa&pwd=123 2 3 //?user=aaa&pwd=123
1 var qs = location.search.length > 0? location.search.substring(1) : '';//user=aaa&pwd=123 2 //从索引 1 开始取到后面的字符
1 var items = qs.length? qs.split('&') : [];//['user=aaa','pwd=123']
1 var item = null,//装 items 中的元素 2 name = null,//装 item 中的名字 3 value = null,//装名字对应的值 4 args = {};//装结果
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"}
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);