通過javascript獲得url參數

來源:互聯網
上載者:User
javascript

頁面提交資料一般有兩種方法:get,post。post就是所謂的form提交,使用視圖;get是通過url提交。

Get方法一般用後台代碼(如asp,asp.net)獲得參數,代碼很簡單:Request.QueryString["id"];即可擷取。 

有些時候需要直接在前台擷取url參數,要用到javascript,js沒有直接擷取url參數的方法,那麼,我們如何通過js擷取url參數呢?
閱讀代碼編輯代碼運行效果複製HTML代碼儲存代碼<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> new document </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
  <script type="text/javascript">
function GetUrlParms()   
{
    var args=new Object();  
    var query=location.search.substring(1);//擷取查詢串  
    var pairs=query.split("&");//在逗號處斷開  
    for(var i=0;i<pairs.length;i++)  
    {  
        var pos=pairs[i].indexOf('=');//尋找name=value  
        if(pos==-1)   continue;//如果沒有找到就跳過  
        var argname=pairs[i].substring(0,pos);//提取name  
        var value=pairs[i].substring(pos+1);//提取value  
        args[argname]=unescape(value);//存為屬性  
    }
    return args;
}
//使用方法:
var args = new Object();
args = GetUrlParms();
//如果要尋找參數ArticleID:
var articleID = args["ArticleID"];
alert(articleID);
//如果要尋找參數TypeID
var typeID = args["TypeID"];
alert(typeID);
</script>
 </head>

 <body>
 
 </body>
</html>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.