js獲取(URL)地址欄參數

 
//獲取地址欄參數 
    //url爲空時爲調用當前url地址 
    //調用方法爲 var params = getPatams();
    function getParams(url) {
        var theRequest = new Object();
        if (!url)
            url = location.href;
        if (url.indexOf("?") !== -1)
        {
            var str = url.substr(url.indexOf("?") + 1) + "&";
            var strs = str.split("&");
            for (var i = 0; i < strs.length - 1; i++)
            {
                var key = strs[i].substring(0, strs[i].indexOf("="));
                var val = strs[i].substring(strs[i].indexOf("=") + 1);
                theRequest[key] = val;
            }
        }
        return theRequest;
    }
    var params = getParams();
    //獲取地址欄上的userName
    var userName =  params.userName;
相關文章
相關標籤/搜索