jQuery: How to get parameters of a url?

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

    from here

    $.urlParam = function(name){
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        return results[1] || 0;
    }
    
    // example.com?param1=name&param2=&id=6
    $.urlParam('param1'); // name
    $.urlParam('id');        // 6
    $.urlParam('param2');   // null
    
    0 讨论(0)
  • 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];
     }
    });
    
    0 讨论(0)
提交回复
热议问题