What is location.search in javascript

I want to know what location.search.substring(1) 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?

alert(location.search.substring(1))

Solution 1:

http://example.com/index.php?foo=bar

location.search
> ?foo=bar
location.search.substring(1)
> foo=bar

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

Solution 2:

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

Example:

http://www.example.org/index.php?param=arg
location.search is ?param=arg

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

Solution 3:

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

That means, that location.search.substring(1) should return the data without the question mark.

// http://www.example.com/index.html
console.log(location.search.substring(1)); // no query string, so displays nothing

// http://www.example.com/index.html?property=value
console.log(location.search.substring(1)); // should display "property=value"

The "query porpotion" is the query string:

http://www.example.com/?property=value&property2=value
                       |        query string         |