替换 window.location当中的某个参数的值(而其它值不变)JS代码
在后台与前台的交互操作中,需要替换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>
替换 window.location当中的某个参数的值(而其它值不变)JS代码的更多相关文章
- Arison [JS]window.location获取url各项参数详解
https://www.cnblogs.com/Arison/p/5286368.html 对于这样一个URL代码如下 复制代码 http://www.php230.com :80/fisker/po ...
- window.location获取url各项参数详解
window.location方法后还还可以带href,search等参数,下面我们来看看获取url各项参数的办法. URL即:统一资源定位符 (Uniform Resource Locator, U ...
- [JS]window.location获取url各项参数详解
window.location方法后还还可以带href,search等参数,下面我们来看看获取url各项参数的办法. URL即:统一资源定位符 (Uniform Resource Locator, U ...
- 关于window.location.href 传中文参数 乱码问题
传中文查询乱码问题 则需要对要传的参数进行二次编码 例如 window.location.href ="/xx.jsp?name="+name+""; 这样子 ...
- window.location.href后携带参数
JS文件中: window.location.href后可携带参数,但是不安全,虽然在技术上是可以实现的 1.传参:window.location.href = "RecordCare.as ...
- javaweb使用 window.location.href 传中文参数 乱码问题
JS: var cn_name= document.getElementById("cn_name"); window.location.href="${URL}?na ...
- Javascript Window Location
window.location 对象在编写时可不使用 window 这个前缀. URL : 统一资源定位符 (Uniform Resource Locator) 说明: 完整的URL示例:scheme ...
- window.location.href问题,点击,跳转到首页
onClick="window.location.href='./';" 点击,跳转到首页. location.href=url Js中实现跳转 window.location.h ...
- window.location获取URL中各部分
博客分类: JAVASCRIPT JavaScriptASP.netSchemeASP网络协议 URL即:统一资源定位符 (Uniform Resource Locator, URL) 完整的URL ...
随机推荐
- O2O领域添新军,正品网加快布局的战略考量
前不久.正品网採购虚拟运营商30万170号码的招标公告引发了业界的广泛关注.一方面,虚拟运营商正处于"徘徊不定"的十字路口.据业内知情人士透露,眼下12家虚拟运营商企业共放号在20 ...
- Codeforces Round #256 (Div. 2/C)/Codeforces448C_Painting Fence(分治)
解题报告 给篱笆上色,要求步骤最少,篱笆怎么上色应该懂吧,.,刷子能够在横着和竖着刷,不能跳着刷,,, 假设是竖着刷,应当是篱笆的条数,横着刷的话.就是刷完最短木板的长度,再接着考虑没有刷的木板,,. ...
- 我的小前端 (1)—— 安卓机和ios机的区别
没有什么特别新技术,就是记录我做移动端遇到的问题 2016-02-16 微信,支付宝和APP都会遇到这些问题 一.安卓机和ios机的区别 1.常用 <head> <me ...
- 在.NET下学习Extjs(第三个案例 Array的过滤方法(filter))
Ext.Array.filter(Array array,Function fn,Object scope):Array array是一个数组,fn是过滤函数,scope是作用域,filter返回的是 ...
- ora-06502
执行一个存储过程时报这个错误 ORA-06502: PL/SQL: 数字或值错误 发现是定义的字符串的缓冲区太小,赋给字符串的值又太大 修改varchar2(20) → varchar2(200 ...
- 学习使用GitHub(一)--之入门
因为经常Windows和linux系统交替的使用,在实验室一台电脑,在家一台电脑,自己的电脑和实验室的电脑上面的代码往往没法同步,以前由于种种原因(其实就是懒,没有学习GitHub这样的代码管理工具) ...
- prob5 of 140
#include<stdio.h>int main(){ int n,i=1,j=1; double s=1,s1=0;; //scanf("%d",&n); ...
- Mongodb基础知识----Mongodb权威指南阅读
文档是Mongodb中数据的基本单元,类型关系型数据库中的行,每个文档都有一个键值唯一的键_id.集合可以看做拥有动态模式的表. Mongodb一个实例可以拥有多个相互独立的数据库. Mongodb区 ...
- The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, SettingFragment, String)
The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable for the ...
- oc swift 混编 特技
1.swift 工程新建oc文件,新建的时候提示是否桥接文件,点击yes,把swift要用的oc文件的头文件 都导入桥接文件中就OK了. 2.在swift工程中oc调用 swift文件,需要在导入名字 ...