URL获取并修改参数
Posted 暗里着迷
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了URL获取并修改参数相关的知识,希望对你有一定的参考价值。
1 function changeURLPar(url, ref, value) { 2 var str = ""; 3 if (url.indexOf(‘?‘) != -1) 4 str = url.substr(url.indexOf(‘?‘) + 1); 5 else 6 return url + "?" + ref + "=" + value; 7 var returnurl = ""; 8 var setparam = ""; 9 var arr; 10 var modify = "0"; 11 if (str.indexOf(‘&‘) != -1) { 12 arr = str.split(‘&‘); 13 for (i in arr) { 14 if (arr[i].split(‘=‘)[0] == ref) { 15 setparam = value; 16 modify = "1"; 17 } 18 else { 19 setparam = arr[i].split(‘=‘)[1]; 20 } 21 returnurl = returnurl + arr[i].split(‘=‘)[0] + "=" + setparam + "&"; 22 } 23 returnurl = returnurl.substr(0, returnurl.length - 1); 24 if (modify == "0") 25 if (returnurl == str) 26 returnurl = returnurl + "&" + ref + "=" + value; 27 } 28 else { 29 if (str.indexOf(‘=‘) != -1) { 30 arr = str.split(‘=‘); 31 if (arr[0] == ref) { 32 setparam = value; 33 modify = "1"; 34 } 35 else { 36 setparam = arr[1]; 37 } 38 returnurl = arr[0] + "=" + setparam; 39 if (modify == "0") 40 if (returnurl == str) 41 returnurl = returnurl + "&" + ref + "=" + value; 42 } 43 else 44 returnurl = ref + "=" + value; 45 } 46 return url.substr(0, url.indexOf(‘?‘)) + "?" + returnurl; 47 }
以上是关于URL获取并修改参数的主要内容,如果未能解决你的问题,请参考以下文章