[Solved] What is in javascript

I want to know what actually does. I saw this code on some website. I tried to print using alert, but this did not give any result. Is it supposed to alert location href?

Enquirer: jchand


Solution #1:
> ?foo=bar
> foo=bar

So that code will return the entire query parameters without the question mark.

Respondent: sachleen

Solution #2:

The property contains the query string of an URI (including the ?), if any.

Example: is ?param=arg

So your code snips away the leading ? and returns param=arg.

Respondent: Gregor

Solution #3:

The search property returns the query portion of a URL, including the question mark (?).

That means, that should return the data without the question mark.

console.log(; // no query string, so displays nothing

console.log(; // should display "property=value"

The “query porpotion” is the query string:
                       |        query string         |
Respondent: Tim S.

Solution #4:

e.g. if you have the following url

then will return ?Browser=Netscape as a string

Respondent: JuHwon

Solution #5: returns a query string including the initial question mark. The substr method is to remove the initial question mark from the returned query string.

The reason why you’re not getting any results in the alert() is because you’re trying to read query string on a website which doesn’t have any.

Here’s how you test:

  1. Go to this URL
  2. Open up the console on your browser
  3. Paste the code snippet shared below and hit enter
var container = {};'&').toString().substr(1).split(",").forEach(item => {
    container[item.split("=")[0]] = decodeURIComponent(item.split("=")[1]) ?  item.split("=")[1]: "No query strings available" ;

Old question but the answer might be helpful for new visitors on this page.

Respondent: Rehan

Solution #6:

Now is year of 2018, this is how you do it in 2018.

Sample URL:


My working code to extract each query parameter:

        var ___zoom;
        var ___lat;
        var ___long;
        var ___basemap;

        var ___type;
        var ___url;
        var ___title;
        var ___opacity;

        if ([^&]*)/i) )
             ___zoom =[^&]*)/i)[1];

        if ([^&]*)/i) )
           ___lat =[^&]*)/i)[1];

        if ([^&]*)/i))
            ___long =[^&]*)/i)[1];

        if ([^&]*)/i))
            ___basemap =[^&]*)/i)[1];

        if ([^&]*)/i))
            ___type =[^&]*)/i)[1];

       if ([^&]*)/i))
            ___url =[^&]*)/i)[1];

        if ([^&]*)/i))
            ___title =[^&]*)/i)[1];

        if ([^&]*)/i))
            ___opacity =[^&]*)/i)[1];

        //console.log([^&]*)/i)[0]);   //    'zoom=17'
        //console.log([^&]*)/i)[1]);   //     '17'

Respondent: hoogw

Solution #7:

It returns the query string, without the initial question mark. You’ll only see a result if there’s a query string on the page, e.g.

Respondent: Nick

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy