1.替换url参数值
- function replaceUrl(url, arg, val){
- var pattern = arg+'=([^&]*)';
- var replaceText = arg+'='+val;
- return url.match(pattern) ? url.replace(eval('/('+ arg+'=)([^&]*)/gi'), replaceText) : (url.match('[?]') ? url+'&'+replaceText : url+'?'+replaceText);
- }
- document.write(replaceUrl('http://www.t.com/test.php?class_id=3&id=2', 'id', 4));
2.截除?后字符串
- function getQMark(my_url){
- //如果url带问号
- var qMark;
- if(my_url.indexOf("?") != -1){
- qMark = my_url.split("?")[1];
- }
- return qMark;
- }
3.获取链接参数
- function GetRequest() {
- var url = location.search; //获取url中"?"符后的字串
- var theRequest = new Object();
- if (url.indexOf("?") != -1) {
- var str = url.substr(1);
- strs = str.split("&");
- for(var i = 0; i < strs.length; i ++) {
- theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
- }
- }
- return theRequest;
- };
- GetRequest()
参考:https://blog.csdn.net/qq_41802862/article/details/88566922