JavaScript操作URL的相關內容集錦,javascript相關內容

來源:互聯網
上載者:User

JavaScript操作URL的相關內容集錦,javascript相關內容

---恢複內容開始---

1.location.href.....

(1)self.loction.href="http://www.cnblogs.com/url"
       window.location.href="http://www.cnblogs.com/url"    以上兩個用法相同均為在當前頁面開啟URL頁面
(2)this.location.href="http://www.cnblogs.com/url"    當前頁面開啟URL
(3) parent.location.href="http://www.cnblogs.com/url"   在父頁面開啟新頁面,如果頁面中自訂了frame,那麼可將parent self top換為自訂frame的名稱,效果是在frame視窗開啟url地址
(4) top.location.href="http://www.cnblogs.com/url"   在頂層頁面開啟新頁面

2. 關於重新整理頁面

 (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提交資料

3.

 (1)第一段為實際在用的

function getURLParameter(name) {2 3       return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null; //構造一個含有目標參數的Regex對象4 5   }//擷取url中的參數2     function getUrlParam(name){3      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //構造一個含有目標參數的Regex對象4      var r = window.location.search.substr(1).match(reg); //匹配目標參數5      if (r != null) return unescape(r[2]); return null; //返回參數值6    }

  例如像擷取下面連結的郵箱

  http://agent/index.php/Home/Login/getpwd_check_email?code=824790&to=1321136493@qq.com

var mail = getURLParameter('to');

---恢複內容結束---

下面再來看一下js操作url的代碼

代碼很簡單,主要一個思路是把url參數解析為js對象,再做增、刪、改、查操作就很方便了~,這裡做筆記。

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只是我個人共同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());

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.