document.location.href //可读写
document.URL //只读
document.documentURL //不支持IE,但可以返回所有类型的文档
_getQueryString = (url, name) => {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = url.substr(1).match(reg);
if (r != null)
return decodeURI(r[2]);
return null;
}
_getQueryParams = (url) => {
var obj = {};
var reg = /[?&][^?&]+=[^?&]+/g;
var arr = url.match(reg);
if (arr) {
arr.forEach(function (item) {
var tempArr = item.substring(1).split('=');
var key = decodeURIComponent(tempArr[0]);
var val = decodeURIComponent(tempArr[1]);
obj[key] = val;
});
}
return obj;
}
const q = {};
location.search.replace(/([^?&=]+)=([^&]+)/g,(_,k,v)=>q[k]=v);
- a 标签赋值 href,获取 pathname,location 等
// 创建a标签
const aEle = document.createElement('a');
// 给a标签赋值href路径
aEle.href = '/test.html';
// 访问aEle中的属性
aEle.protocol; // 获取协议
aEle.pathname; // 获取path
aEle.origin;
aEle.host;
aEle.search;