jQuery: How to get parameters of a url?

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

    Your jquery could be

    $(function() {  
     $('.popuptest a').live('click', function(event) {
     $.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]);
        vars[hash[0]] = hash[1];
      }
      return vars;
    },
    getUrlVar: function(name){
      return $.getUrlVars()[name];
    }
      });
      var second = $.getUrlVars()["param2"];
      alert(second);
      return false;
      });
    });
    

    things to note:

    1. $.getUrlVars in line - var second = $.getUrlVars()["param2"];
    2. this.href is replaced to $('.popuptest a').attr("href")
    0 讨论(0)
  • 2020-12-05 08:14

    it's so easy, all you have to do is put the variable name that you want it from the URL into this function then it will return you the value of URL variable

    function getParameterByName(name) {
     return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
    }
    
    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2020-12-05 08:16

    You don't need jQuery for that purpose you can use the pure JavaScript:

    function getParameterByName( name,href )
    {
      name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      var regexS = "[\\?&]"+name+"=([^&#]*)";
      var regex = new RegExp( regexS );
      var results = regex.exec( href );
      if( results == null )
        return "";
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    

    and you can call the function in this way..getParameterByName(param1,href of your link)

    DEMO

    0 讨论(0)
  • 2020-12-05 08:17
    function getURLParameter(name) {
        return decodeURI(
            (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
        );
    }
    
    0 讨论(0)
  • 2020-12-05 08:19

    So simple you can use any url and get value in Javascript.And this is Complete set of codes

    <!DOCTYPE html>
    <html>
    <head>
    <script>
    var tech = getQueryVariable('filename');
    function getQueryVariable(variable)
    {
           var query = window.location.search.substring(1);
           var vars = query.split("&");
           for (var i=0;i<vars.length;i++) {
                   var pair = vars[i].split("=");
                   if(pair[0] == variable){return pair[1];}
           }
           return(false);
    }
    alert(tech);
    </script>
    </head>
    <body>
    <p>Get parameters of a url</p>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题