JS 获取链接中的参数

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]);
        }
    }
    return theRequest;
 }

 

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;
 }

 

posted @ 2018-11-07 16:53  深海地平线  阅读(14432)  评论(0编辑  收藏  举报