Home > Article > Web Front-end > Collection of related content on JavaScript operation URL_javascript skills
---Restore content starts---
1.location.href.....
(1) self.location.href="http://www.cnblogs.com/url"
window.location.href="http://www.cnblogs.com/url" The above two usages are the same, both open the URL page on the current page
(2) this.location.href="http://www.cnblogs.com/url" Current page opens URL
(3) parent.location.href="http://www.cnblogs.com/url" Open a new page on the parent page. If the frame is customized in the page, you can change parent self top To customize the name of the frame, the effect is to open the url address in the frame window
(4) top.location.href="http://www.cnblogs.com/url" Open a new page on the top page
2. About refreshing the page
(1) window.location.href=http://www.cnblogs.com/nana-share/p/window.location.href
(2) window.location.Reload()
They all refresh the current page. The difference lies in whether data is submitted. When data is submitted, window.location.Reload() will prompt whether to submit it. window.location.href=http://www.cnblogs.com/nana-share/p/window.location.href; is to specify The url submits the data
3.
(1) The first paragraph is actually used
function getURLParameter(name) {2 3 return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null; //构造一个含有目标参数的正则表达式对象4 5 } //获取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 }
For example, get the email address of the link below
http://agent/index.php/Home/Login/getpwd_check_email?code=824790&to=1321136493@qq.com
var mail = getURLParameter('to');
---End of recovery content---
Let’s take a look at the code for js operation url
The code is very simple. The main idea is to parse the url parameters into js objects, and then it is very convenient to add, delete, modify and check~, take notes here.
var LG=(function(lg){ var objURL=function(url){ this.ourl=url||window.location.href; this.href="";//?前面部分 this.params={};//url参数对象 this.jing="";//#及后面部分 this.init(); } //分析url,得到?前面存入this.href,参数解析为this.params对象,#号及后面存入this.jing objURL.prototype.init=function(){ var str=this.ourl; var index=str.indexOf("#"); if(index>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;i<parts.length;i++){ var kv=parts[i].split("="); this.params[kv[0]]=kv[1]; } } else{ this.href=this.ourl; this.params={}; } } //只是修改this.params objURL.prototype.set=function(key,val){ this.params[key]=val; } //只是设置this.params objURL.prototype.remove=function(key){ this.params[key]=undefined; } //根据三部分组成操作后的url objURL.prototype.url=function(){ var strurl=this.href; var objps=[];//这里用数组组织,再做join操作 for(var k in this.params){ if(this.params[k]){ objps.push(k+"="+this.params[k]); } } if(objps.length>0){ strurl+="?"+objps.join("&"); } if(this.jing.length>0){ strurl+=this.jing; } return strurl; } //得到参数值 objURL.prototype.get=function(key){ return this.params[key]; } lg.URL=objURL; return lg; }(LG||{}));
LG is just my personal common JS namespace, nothing else. Call:
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());