0
点赞
收藏
分享

微信扫一扫

【归总】原生js操作浏览器hash、url参数参数获取/修改方法合集


$g.$utils.hash = {
get(win) {
return decodeURIComponent((win || top).location.hash.substr(1));
},
getSearchAndHash(win) {
win || (win = top);
return decodeURIComponent(win.location.search + win.location.hash);
},
set(hash, win, isReload) {
hash || (hash = "");
win = win || top;
win.location.hash = hash;
isReload && win.location.reload();
},

addListener(win) {
(win || window).onhashchange = function () {
this.location.reload();
}
},

getUrlParam(name, win, isEncode) {
var r = (win || top).location.search.substr(1).match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)"));
if (r != null) {
return isEncode ? r[2] : decodeURIComponent(r[2]);
}
return null;
},
getUrlParams(url) {
var reg = /([^=&\s]+)[=\s]*([^&\s]*)/g, ls = url || location.search.substr(1), obj = {};
while (reg.exec(ls)) obj[RegExp.$1] = decodeURIComponent(RegExp.$2);
return obj;
},

setUrlSearch(search, win, isReload) { /*此方法可以点击网页返回按钮*/
win = win || top;
win.history.pushState(null, null, search || "./");
isReload && win.location.reload();
},
replceUrlSearch(search, win, isReload) { /*此方法无网页返回按钮行为*/
win = win || top;
win.history.replaceState(null, null, search || "./");
isReload && win.location.reload();
},

getFileName() {
var lp = location.pathname, fn = lp.substr(lp.lastIndexOf("/") + 1);
return fn.substr(0, fn.lastIndexOf("."));
}
};


举报

相关推荐

0 条评论