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

时间:2022-01-07 22:11:20

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

说明:

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

 <script type='text/javascript'>
//替换指定传入参数的值
//win为某窗体,paramName为参数,paramValue为新值,forceAdding为不存在该参数时是否也指定
function replaceParamVal(win,paramName,paramValue,forceAdding){
var search = win.location.search+'';
if(!search) {//没有任何查询参数,则直接附加
return ( forceAdding ? (win.location+'?'+paramName+'='+paramValue) : (win.location+'') );
}else{
var q = (win.location+'').split('?')[0];
var list = search.replace('?','').split('&');
var hasIn = false;
for(var i=0; i<list.length; i++) {
var listI = list[i];
if(listI.split('=')[0].toLowerCase() == paramName.toLowerCase()) {//指定参数
q = q + (i==0 ? '?' : '&');
hasIn = true; if(listI.indexOf('=') == -1) {//形式:"参数"
q = q + listI + '=' + paramValue;
}
else if (! listI.split('=')[1].length) { //形式:"参数="
q = q + listI + paramValue;
}
else {//形式:"参数=值"
q = q + paramName + '=' + paramValue;
}
}else {//其它参数
q = q + (i==0 ? '?' : '&');
q = q + listI;
}
} if (!hasIn && forceAdding) {//不存在,但必须要添加时
q = q + '&' + paramName + '=' + paramValue;
} return q;
} }
</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的传入值,方便各种场合:

 <script type='text/javascript'>
//替换指定传入参数的值
//win为某窗体或查询字符串,paramName为参数,paramValue为新值,forceAdding为不存在该参数时是否也指定
function replaceParamVal(win,paramName,paramValue,forceAdding){
var search = (typeof win == 'object') ? (win.location.search+'') : (win={location:win},win.location); //改动for字符串的win
if(!search) {//没有任何查询参数,则直接附加
return ( forceAdding ? (win.location+'?'+paramName+'='+paramValue) : (win.location+'') );
}else{
var q = (win.location+'').split('?')[0];
var list = search.indexOf('?') == -1 ? [] : search.split('?')[1].split('&'); //改动for字符串的win
var hasIn = false;
var hasQ = false;//改动for字符串的win
for(var i=0; i<list.length; i++) {
var listI = list[i];
if(listI.split('=')[0].toLowerCase() == paramName.toLowerCase()) {//指定参数
q = q + (i==0 ? '?' : '&');
hasIn = true; if(listI.indexOf('=') == -1) {//形式:"参数"
q = q + listI + '=' + paramValue;
}
else if (! listI.split('=')[1].length) { //形式:"参数="
q = q + listI + paramValue;
}
else {//形式:"参数=值"
q = q + paramName + '=' + paramValue;
}
}else {//其它参数
q = q + (i==0 ? '?' : '&');
q = q + listI; if(listI.indexOf('=') == -1) {//形式:"参数" //改动for字符串的win
hasQ = true;//改动for字符串的win
}
}
} if (!hasIn && forceAdding) {//不存在,但必须要添加时
q = q + (q.indexOf('&') == -1 ? (hasQ ? '&' : '?') : '&') + paramName + '=' + paramValue; //改动for字符串的win
} return q;
} } //另外,可将该函数扩展到string去
String.prototype.reParam = function(paramName,paramValue,forceAdding) {
return replaceParamVal(this.toString(),paramName,paramValue,forceAdding);
}
</script> <script>
window.document.write('<br/>','a&b=2'.reParam('rnd',encodeURIComponent(Date()+''),true))
window.document.write('<br/>','?a=1&b=2'.reParam('rnd',encodeURIComponent(Date()+''),true))
window.document.write('<br/>','B.asp?a=1&b=2&rnd=xxxxxxx'.reParam('rnd',encodeURIComponent(Date()+''),true))
window.document.write('<br/>','http://localhost:8888/index.asp?a=1&b=2&rnd='.reParam('rnd',encodeURIComponent(Date()+''),true))
window.document.write('<br/>',window.location.toString().reParam('rnd',encodeURIComponent(Date()+''),true))
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))
</script>