jQuery: How to get parameters of a url?

后端 未结 8 837
离开以前
离开以前 2020-12-05 07:42

New to jQuery and I\'m having trouble getting the parameters of a url that my server generates. I have a url like this:



        
8条回答
  •  再見小時候
    2020-12-05 08:15

    small improvement, parse the url only once, return array or params:

    function getURLParameters(url){
    
        var result = {};
        var hashIndex = url.indexOf("#");
        if (hashIndex > 0)
           url = url.substr(0, hashIndex);        
        var searchIndex = url.indexOf("?");
        if (searchIndex == -1 ) return result;
        var sPageURL = url.substring(searchIndex +1);
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++)
        {       
            var sParameterName = sURLVariables[i].split('=');      
            result[sParameterName[0]] = sParameterName[1];
        }
        return result;
    }
    

    http://jsfiddle.net/shakhal/gXM3u/

提交回复
热议问题