替换 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 ...
随机推荐
- 查询最小未使用ID的SQL查询
--每个都加一,以此来找出最小的未用ID SELECT Min(T1.ID)+1 FROM dbo.TestTable T1 -- 不用查询已经存在的ID WHERE (T1.ID+1) NOT IN ...
- jquery单页网站导航插件One Page Nav
这是一个轻量级的jQuery的单页网站导航插件.增加了单击后平滑滚动导航和当你浏览不同的部分时自动选择正确的导航项. changeHash: false, 改变当用户单击导航,就改变changeHas ...
- 在.NET下学习Extjs(第三个案例 Array的过滤方法(filter))
Ext.Array.filter(Array array,Function fn,Object scope):Array array是一个数组,fn是过滤函数,scope是作用域,filter返回的是 ...
- c++中参数传递和函数返回简析
1.参数传递: 每次调用函数时,都会重新创建该函数所有的形参,此时所传递的实参将会初始化对应的形参.形参的初始化与变量的初始化一样. 非引用类型:如果形参具有非引用类型,则复制实参的值.普通的非引用类 ...
- C#中一个问号和两个问号(a ?? b)的作用
不卖关子,直接开门见山: C#中两个问号的作用是判断??左边的对象是否为null,如果不为null则使用??左边的对象,如果为null则使用??右边的对象. 比如:a = b ?? c,如果b为nul ...
- 教你如何利用初级C#语言更改银行存款!!!!
您是否对生活现状不满意? 您是否总是感叹工资太少? 您是否经常发现自己相中的物品又降价了然而自己却还是只能望洋兴叹? 没关系!让我来拯救你的钱包! 接下来进入正题: 要想更改自己的银行存款首先得找到一 ...
- TreeSet()详解
TreeSet()详解 1.TreeSet原理: /* * TreeSet存储对象的时候, 可以排序, 但是需要指定排序的算法 * * Integer能排序(有默认顺序), String能排 ...
- php simple_html_dom 一个iconv错误引起解析中断的问题,貌似内存溢出
环境: $pageNum = 8; for ($i = 1; $i < $pageNum; $i++) { $html = new simple_html_dom(); $host = 'htt ...
- 添加nginx为系统服务(service nginx start/stop/restart)
1.在/etc/init.d/目录下编写脚本,名为nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # ...
- sheelエラー、オブジェクトを解析中にエラーが発生しました。