jQuery: How to get parameters of a url?

后端 未结 8 840
离开以前
离开以前 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:39

    Using code below for some time, but recently discoverd a problem with an base64_encode string in the URL, having == at the end, like: /index.php?a=1&b=2&c=Mw==&d=4.

    Using getUrlVar("c") gives me 'Mw' as result, but this should be 'Mw==', so added an extra line:

     $.extend({
      getUrlVars: function(){
       var vars = [], hash;
       var hashes = $('.popuptest a').attr("href").slice($('.popuptest a').attr("href").indexOf('?') + 1).split('&');
       for(var i = 0; i < hashes.length; i++)
       {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        if (hash.length > 2) hash[1] = hash.slice(1).join('=');
        vars[hash[0]] = hash[1];
       }
       return vars;
      },
      getUrlVar: function(name){
       return $.getUrlVars()[name];
     }
    });
    

提交回复
热议问题