1. 程式人生 > >JS 獲取鏈接中的參數

JS 獲取鏈接中的參數

split turn 獲取 字串 pan != return oca quest

1、獲取鏈接全部參數,以對象的形式返回

//獲取url中參數
function GetRequest()
 {
    var url = location.search;   //獲取url中"?"符後的字串   
    var theRequest = new Object();
    if (url.indexOf("?") != -1) 
    {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) 
        {
            theRequest[strs[i].split(
"=")[0]] = unescape(strs[i].split("=")[1]); } } }

2、根據參數名獲取鏈接中的參數值

function GetRequest2(key)
 {
    var url = location.search;  
    var theRequest = new Object();
    if (url.indexOf("?") != -1) 
    {
        var str = url.substr(1);
        strs = str.split("&");
        for (var
i = 0; i < strs.length; i++) { theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]); } } var value = theRequest[key]; return value; }

JS 獲取鏈接中的參數