Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I have an example URL, something like this

http://localhost/PMApp/temp.htm?ProjectID=462

What I need to do is to get the details after the ? sign - that is ProjectID=462. How can I get that using JavaScript?

What I've done so far is this:

var url = window.location.toString();
url.match(?);

I don't know what to do next.

share|improve this question

marked as duplicate by Cupcake, user568109, Daniel Lyons, Donal Fellows, fedorqui Aug 1 '13 at 12:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Check this stackoverflow.com/questions/901115/… –  slash197 Mar 26 '12 at 10:33
    
@Cupcake: That question is about extracting parameters, this here only about getting location.search –  Bergi Aug 1 '13 at 0:23

4 Answers 4

up vote 18 down vote accepted

Have a look at the MDN article about window.location.

The QueryString is available in window.location.search.

MDN also provide an example of how to the get value of a single key available in the QueryString. Something like this:

function getQueryStringValue (key) {  
  return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));  
}  

// Would write the value of the QueryString-variable called name to the console  
console.log(getQueryStringValue("name")); 
share|improve this answer

Use window.location.search to get everything after ? including ?

Example:

var url = window.location.search;
url = url.replace("?", ''); // remove the ?
alert(url); //alerts ProjectID=462 is your case
share|improve this answer

This is will add a global to access the queryString variables as a map.

// -------------------------------------------------------------------
// Add prototype for 'window.location.query([source])' which contain an object
// of querystring keys and their values
// -------------------------------------------------------------------
if(!window.location.query) {
    window.location.query = function(source){
        var map = {};
        source = source || this.search;

        if ("" != source) {
            var groups = source.substr(1).split("&"), i;

            for (i in groups) {
                i = groups[i].split("=");
                map[decodeURIComponent(i[0])] = decodeURIComponent(i[1]);
            }
        }

        return map;
    };
}

Enjoy.

share|improve this answer

You can use the search property of the window.location object to obtain the query part of the URL. Note that it includes the question mark (?) at the beginning, just in case that affects how you intend to parse it.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.