URL擷取參數值

來源:互聯網
上載者:User

標籤:tar   arch   地址欄   內容   arc   cat   tle   bst   ring   

用JS擷取地址欄參數的方法(超級簡單)

方法一:採用Regex擷取地址欄參數:( 強烈推薦,既實用又方便!)

function GetQueryString(name){     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");     var r = window.location.search.substr(1).match(reg);     if(r!=null)return  unescape(r[2]); return null;} // 調用方法alert(GetQueryString("參數名1"));alert(GetQueryString("參數名2"));alert(GetQueryString("參數名3"));

 

下面舉一個例子:

若地址欄URL為:abc.html?id=123&url=http://www.maidq.com

那麼,但你用上面的方法去調用:alert(GetQueryString("url"));

則會彈出一個對話方塊:內容就是 http://www.maidq.com

如果用:alert(GetQueryString("id"));那麼彈出的內容就是 123 啦;

當然如果你沒有傳參數的話,比如你的地址是 abc.html 後面沒有參數,那強行輸出調用結果有的時候會報錯:

所以我們要加一個判斷 ,判斷我們請求的參數是否為空白,首先把值賦給一個變數:

var myurl=GetQueryString("url");if(myurl !=null && myurl.toString().length>1){   alert(GetQueryString("url"));}

這樣就不會報錯了!

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.