What is location.search in javascript

window.location.search get parameters
location javascript mdn
location host
window.location.search is empty
window.location.search not working
typescript location
urlsearchparams javascript
javascript query

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?


> ?foo=bar
> foo=bar

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

Location: search, The search property of the Location interface is a search string, also called a query string; that is, a USVString containing a '?' followed by the  The search property sets or returns the querystring part of a URL, including the question mark (?). The querystring part is the part of the URL after the question mark (?). This is often used for parameter passing. Browser Support. Return the search property: location.search. Set the search property: location.search = querystring. Property Values.

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


location.search is ?param=arg

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

window.location.search · WebPlatform Docs, Note: This property is read-only. var result = window.location.search;. Return Value. Returns an object of type StringString. The  How to determine the current location in JavaScript The location object provides access to a number of properties that let you interact with or dissect the complete URL. The href property displays the entire URL. Properties such as search and port provide access to URL components.

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:

                       |        query string         |

What is location.search in javascript, http://example.com/index.php?foo=bar location.search > ?foo=bar location.​search.substring(1) > foo=bar. So that code will return the entire  10 Answers 10. Here's a pure JS function. Parses the search part of the current URL and returns an object. (It's a bit verbose for readability, mind.) On a related note, you're not trying to store the single parameters in "a JSON" but in "an object".

e.g. if you have the following url


then window.location.search will return ?Browser=Netscape as a string

JavaScript Window Location, Similarly, you can use other properties of the location object such as protocol , hostname , port , pathname , search , etc. to obtain different part of the URL. The above HTML fragment is the basic structure of the search page. It creates a JavaScript global variable, pages, and uses Jekyll data to build out the values from site content pages. Now we need to index our serialized page data with lunr. We'll handle our custom search logic in a separate search.js script.

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 ( location.search.match(/zoom=([^&]*)/i) )
             ___zoom = location.search.match(/zoom=([^&]*)/i)[1];

        if ( location.search.match(/lat=([^&]*)/i) )
           ___lat = location.search.match(/lat=([^&]*)/i)[1];

        if (location.search.match(/long=([^&]*)/i))
            ___long = location.search.match(/long=([^&]*)/i)[1];

        if (location.search.match(/basemap=([^&]*)/i))
            ___basemap = location.search.match(/basemap=([^&]*)/i)[1];

        if (location.search.match(/type=([^&]*)/i))
            ___type = location.search.match(/type=([^&]*)/i)[1];

       if (location.search.match(/url=([^&]*)/i))
            ___url = location.search.match(/url=([^&]*)/i)[1];

        if (location.search.match(/title=([^&]*)/i))
            ___title = location.search.match(/title=([^&]*)/i)[1];

        if (location.search.match(/opacity=([^&]*)/i))
            ___opacity = location.search.match(/opacity=([^&]*)/i)[1];

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


A Complete Guide to the document.location Property and Location , Guide to the document.location Property and Location Object in JavaScript Even though it's a read-only Location object, if we assign a string to it, it will Location.port; Location.pathname; Location.search; Location.hash  JavaScript Window Location. The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page. Window Location. The window.location object can be written without the window prefix.

Get Query String Parameters with JavaScript, We've always been able to get the full query string via the window.location.​search property: console.log(window.location.search);  A WHOLE NUMBER BELOW 0 when the text passed in the function is not in whatever variable or string you are looking for - in this case document.location.search. A WHOLE NUMBER EQUAL TO 0 OR HIGHER when the text passed in the function is in whatever variable or string you are looking for - in this case document.location.search.

Location.search : Location « Location « JavaScript Tutorial, For example, the search for http://www.java2s.com/search.htm?term=javascript is ?term=javascript. <html> <head> <title> Using the search property of the Location​  var n = str.search("W3Schools"); Try it Yourself » More "Try it Yourself" examples below. Definition and Usage. The search () method searches a string for a specified value, and returns the position of the match. The search value can be string or a regular expression. This method returns -1 if no match is found.

Quick Tip: Get URL Parameters with JavaScript, const queryString = window.location.search; console.log(queryString); // ?product​=shirt&color=blue&newuser&size=m. We can then parse the  Location Object. The location object contains information about the current URL. The location object is part of the window object and is accessed through the window.location property. Note: There is no public standard that applies to the location object, but all major browsers support it.

  • location.search returns the query portion of a URL including the Question mark (?). This return a string and then we do substring operation on that string. substring(1) means return the string skipping the first character. I our case "the question mark".
  • w3schools.com/jsref/prop_loc_search.asp
  • I do not know it is incorrect thats why i posted question here
  • @jchand your question isn't incorrect - Tim's answer is what's incorrect. My other comment was in reply to his answer.
  • Your first sentence doesn't match your example - location.search includes the ?.
  • can you please describe what is query portion on href and how it look like
  • I am trying to alert it like alert(window.location.search); I am writing some query string in the url and then click to see alert but it doesn't show any thing. The alert pops up but showing nothing. what could be causing that?
  • I would need an exact example of your url and query to reproduce your issue. Though u also could try the debug mode of your browser. set a breakpoint at the line you want and just type window.location.search or window.location in your console. Its a much better approach than alert to see whats goin on.