微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JavaScript操作URL的相关内容集锦

---恢复内容开始---

(1)self.loction.href="" window.location.href="" 以上两个用法相同均为在当前页面打开URL页面 (2)this.location.href="" 当前页面打开URL (3) parent.location.href="" 在父页面打开新页面,如果页面自定义了frame,那么可将parent self top换为自定义frame的名称,效果是在frame窗口打开URL地址 (4) top.location.href="" 在顶层页面打开新页面

 (1)window.location.href=http://www.cnblogs.com/nana-share/p/window.location.href  (2)window.location.Reload() 都是刷新当前页面。区别在于是否有提交数据。当有提交数据时,window.location.Reload()会提示是否提交,window.location.href=http://www.cnblogs.com/nana-share/p/window.location.href;则是向指定的url提交数据

(1)第一段为实际在用的

//获取url中的参数2
function getUrlParam(name)
{3
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象4
var r = window.location.search.substr(1).match(reg); //匹配目标参数5
if (r != null) return unescape(r[2]); return null; //返回参数值6
}

例如像获取下面链接的邮箱

  

var mail = getURLParameter('to');

---恢复内容结束---

下面再来看一下js操作url的代码

代码很简单,主要一个思路是把url参数解析为js对象,再做增、删、改、查操作就很方便了~,这里做笔记。

0){ this.jing=str.substr(index); str=str.substring(0,index); } index=str.indexOf("?"); if(index>0){ this.href=str.substring(0,index); str=str.substr(index+1); var parts=str.split("&"); for(var i=0;i0){ strurl+=this.jing; } return strurl; } //得到参数值 objURL.prototype.get=function(key){ return this.params[key]; } lg.URL=objURL; return lg; }(LG||{}));

LG只是我个人共同JS的名称空间,无他。调用

rush:js;"> var myurl=new LG.URL("http://www.baidu.com?a=1"); myurl.set("b","hello"); //添加了b=hello alert (myurl.url()); myurl.remove("b"); //删除了b alert(myurl.get ("a"));//取参数a的值,这里得到1 myurl.set("a",23); //修改a的值为23 alert (myurl.url());

原文地址:https://www.jb51.cc/js/51943.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐