1 回答
TA贡献1801条经验 获得超8个赞
我找到了!我应该这个代码:
function getParameter(name,href){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( href );
if( results == null )
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
function setUrlParameter(name,value){
var url = window.location;
var href = url.href;
var search = url.search;
var val = getParameter(name,href);
if(val != ''){
var param = name+'='+val;
var new_param = name+'='+value;
var newUrl = href.replace(param,new_param);
window.history.pushState("string", "Title", newUrl);
return false;
}
else if(search == ''){
var param = '?'+name+'='+value;
var newUrl = href+param;
window.history.pushState("string", "Title", newUrl);
return false;
}
else if(search != ''){
var param = '&'+name+'='+value;
var newUrl = href+param;
window.history.pushState("string", "Title", newUrl);
return false;
}
}
console.log(setUrlParameter("status","1"));
添加回答
举报