How to read get request using Javascript?

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 args to JS? How to parse args from JS?

(not using any frameworks like Jquery, working in IE6, FireFox 3)

Answers:

Answer

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 
}
Answer

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".

Answer

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.

Answer

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).

Answer

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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.