替换 window.location当中的某个参数的值(而其它值不变)JS代码

在后台与前台的交互操作中,需要替换location当中的某个参数的值(而其它值不变)时,会用到以下函数:

 说明:

win:传入的窗口句柄,如,window或window.parent等
forceAdding:当location当中的指定参数不存在时,是无法进行替换操作的,这时候,如果非要"替换"(即将参数名及其值附加上去),则可指定forceAdding为true(或非"零"性质的其它值),否则将不会执行替换操作.

 1 <script type='text/javascript'>
 2 //替换指定传入参数的值
 3 //win为某窗体,paramName为参数,paramValue为新值,forceAdding为不存在该参数时是否也指定
 4 function replaceParamVal(win,paramName,paramValue,forceAdding){
 5     var search = win.location.search+'';
 6     if(!search) {//没有任何查询参数,则直接附加
 7         return ( forceAdding ? (win.location+'?'+paramName+'='+paramValue) : (win.location+'') );
 8     }else{
 9         var q = (win.location+'').split('?')[0];
10         var list = search.replace('?','').split('&');
11         var hasIn = false;
12         for(var i=0; i<list.length; i++) {
13             var listI = list[i];
14             if(listI.split('=')[0].toLowerCase() == paramName.toLowerCase()) {//指定参数
15                 q = q + (i==0 ? '?' : '&');
16                 hasIn = true;
17                 
18                 if(listI.indexOf('=') == -1) {//形式:"参数"
19                     q = q + listI + '=' + paramValue;
20                 }
21                 else if (! listI.split('=')[1].length) {    //形式:"参数="
22                     q = q + listI + paramValue;
23                 }
24                 else {//形式:"参数=值"
25                     q = q + paramName + '=' + paramValue;
26                 }
27             }else {//其它参数                
28                 q = q + (i==0 ? '?' : '&');
29                 q = q + listI;
30             }
31         }
32 
33         if (!hasIn && forceAdding) {//不存在,但必须要添加时
34             q = q + '&' + paramName + '=' + paramValue;
35         }
36 
37         return q;
38     }    
39     
40 }
41 </script>

 调用示例:

window.parent.location = replaceParamVal(window.parent,'rnd',encodeURIComponent(Date()+''),true)

 



个人小记:编写本函数主要原因,是在FF当中,由页面A中包含有一个iframe,利用其src的更换变化(指向页面B) 执行更新操作,操作后利用iframe执行A窗口的reload,这时在FF会导致连续刷新(在IE/Chrome等不会有此现象),仿约代码如:

A.html

<input type='button' value='执行更新' onclick='javascript:save();'/>
<script>
function save() {
    document.getElementById('F').src='B.html?'+Date();
}
</script>
<iframe id="F" src="" width="200" height="200" frameborder="0"></iframe>

B.html

<script>
window.parent.alert('更新了!');
window.parent.document.getElementById('F').src = '';    //在以为加上这句可以解决,事实是,不灵!
window.parent.location.reload();
</script>


所以,最后还是想到另外的reload方式,就是替换随机指定的参数,于是有了以上函数,此时的B页面的代码如:(已省略replaceParamVal函数)

B.html

<script>
window.parent.alert('更新了!');
window.parent.location = replaceParamVal(window.parent,'rnd',encodeURIComponent(Date()+''),true)
</script>

弱弱地扩展了下该函数,支持字符串或window类型参数第一个参数win的传入值,方便各种场合:

 1 <script type='text/javascript'>
 2 //替换指定传入参数的值
 3 //win为某窗体或查询字符串,paramName为参数,paramValue为新值,forceAdding为不存在该参数时是否也指定
 4 function replaceParamVal(win,paramName,paramValue,forceAdding){
 5     var search = (typeof win == 'object') ? (win.location.search+'') : (win={location:win},win.location);    //改动for字符串的win
 6     if(!search) {//没有任何查询参数,则直接附加
 7         return ( forceAdding ? (win.location+'?'+paramName+'='+paramValue) : (win.location+'') );
 8     }else{
 9         var q = (win.location+'').split('?')[0];
10         var list = search.indexOf('?') == -1 ? [] : search.split('?')[1].split('&');    //改动for字符串的win
11         var hasIn = false;
12         var hasQ = false;//改动for字符串的win
13         for(var i=0; i<list.length; i++) {
14             var listI = list[i];
15             if(listI.split('=')[0].toLowerCase() == paramName.toLowerCase()) {//指定参数
16                 q = q + (i==0 ? '?' : '&');
17                 hasIn = true;
18                 
19                 if(listI.indexOf('=') == -1) {//形式:"参数"
20                     q = q + listI + '=' + paramValue;
21                 }
22                 else if (! listI.split('=')[1].length) {    //形式:"参数="
23                     q = q + listI + paramValue;
24                 }
25                 else {//形式:"参数=值"
26                     q = q + paramName + '=' + paramValue;
27                 }
28             }else {//其它参数                
29                 q = q + (i==0 ? '?' : '&');
30                 q = q + listI;
31 
32                 if(listI.indexOf('=') == -1) {//形式:"参数"   //改动for字符串的win
33                     hasQ = true;//改动for字符串的win
34                 }
35             }
36         }
37 
38         if (!hasIn && forceAdding) {//不存在,但必须要添加时
39             q = q + (q.indexOf('&') == -1 ? (hasQ ? '&' : '?') : '&') + paramName + '=' + paramValue;    //改动for字符串的win
40         }
41 
42         return q;
43     }    
44     
45 }
46 
47 //另外,可将该函数扩展到string去
48 String.prototype.reParam = function(paramName,paramValue,forceAdding) {
49     return replaceParamVal(this.toString(),paramName,paramValue,forceAdding);
50 }
51 </script>
52 
53 <script>
54 window.document.write('<br/>','a&b=2'.reParam('rnd',encodeURIComponent(Date()+''),true))
55 window.document.write('<br/>','?a=1&b=2'.reParam('rnd',encodeURIComponent(Date()+''),true))
56 window.document.write('<br/>','B.asp?a=1&b=2&rnd=xxxxxxx'.reParam('rnd',encodeURIComponent(Date()+''),true))
57 window.document.write('<br/>','http://localhost:8888/index.asp?a=1&b=2&rnd='.reParam('rnd',encodeURIComponent(Date()+''),true))
58 window.document.write('<br/>',window.location.toString().reParam('rnd',encodeURIComponent(Date()+''),true))
59 window.document.write('<br/>','file:///C:/Users/Administrator/Desktop/B.html?Wed%20Jul%2017%202013%2017:56:04%20GMT+0800'.reParam('rnd',encodeURIComponent(Date()+''),true))
60 </script>
原文地址:https://www.cnblogs.com/dreamyoung/p/3196025.html