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 }

 

posted @ 2017-02-18 16:30  Liu66~  阅读(212)  评论(0编辑  收藏  举报