1纳寂、 獲取url
<script>
console.log(window.location.href);//獲取url全部信息
console.log(window.location.host);//獲取主機名加上端口號
console.log(window.location.hostname);//獲取主機名
console.log(window.location.port);//獲取端口號
console.log(window.location.hash);//
</script>
在控制臺上打印的結(jié)果如下:
image.png
2、獲取url中的參數(shù)
以下是將一個url 轉(zhuǎn)換為一個map結(jié)構(gòu)
//切割字符串轉(zhuǎn)換參數(shù)表
function toParamMap(str){
var map = {};
var segs = str.split("&");
for(var i in segs){
var seg = segs[i];
var idx = seg.indexOf('=');
if(idx < 0){
continue;
}
var name = seg.substring(0, idx);
var value = seg.substring(idx+1);
map[name] = value;
}
return map;
}