How to read get request using Javascript?

后端 未结 5 762
孤城傲影
孤城傲影 2021-01-06 00:49

So I have html page called A.html it was called like this from B.html : A.html?varString=\"bla-bla-bla\" Is it correct for sending ar

相关标签:
5条回答
  • 2021-01-06 01:04

    Mostly you'd like to handle the parameters passed to your page in the server side, but if you got your reasons why to do it client-side, here's a small script i found:

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

    i didn't test it, but i'm pretty sure it'll to the job.

    just use it like: gup('parameter') and it'll return the parameter value for you.

    0 讨论(0)
  • 2021-01-06 01:12

    Use location.search:

    alert(location.search); // will display everything from the ? onwards
    

    You probably want to separate the different variables from the query string so that you can access them by name:

    var request = {};
    var pairs = location.search.substring(1).split('&');
    for (var i = 0; i < pairs.length; i++) {
      var pair = pairs[i].split('=');
      request[pair[0]] = pair[1];
    }
    

    Then you can access it like request['varString'] and that will give you "bla-bla-bla".

    0 讨论(0)
  • 2021-01-06 01:16

    Here is a function to parse the query string. Pass it the parameter name and it returns the value.

    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 -1; //not found 
    }
    
    0 讨论(0)
  • 2021-01-06 01:20

    Thanks to the new URLSearchParams interface, it becomes easier:

    var url = new URL("https://example.org/?foo=bar&foo2=bar2");
    var params = url.searchParams;
    
    // Access to a variable
    console.log(params.get("foo"));
    
    // Loop over params
    for (var key of params.keys()) {
        console.log(params.get(key));
    }
    

    You should check Mozilla Developer Network for browser compatibility, since it's a new API.

    0 讨论(0)
  • 2021-01-06 01:21

    To parse the query string through JS, you can view use something like

    function getQueryValue(param) {
            var queryStringArray = querystring && querystring.substring(1).split("&");
            for (var i=0, length = queryStringArray.length; i < length; i++) {
                 var token = queryStringArray[i],
                     firstPart = token && token.substring(0, token.indexOf("="));
                 if (firstPart === param ) {
                     return token.substring(token.indexOf("=") + 1, token.length);
                }
            }
     }
    

    E.g. Given a URL "http://domain.com.au?aaa=bbb , you can call this fn as getQeuryValue("aaa") and you'll get "bbb"

    I uploaded this code on Gist (bit modified to be compliant with a module pattern).

    0 讨论(0)
提交回复
热议问题