Alex Rivera | Logout

jquery - check if query string present on URL?

Asked 2010-07-12T20:58:17.213
28

Is it possible using jquery (or just javascript) to check for the existence of a query string on the URL?

Edit
Report

2 Answers

32

document.location contains information about the URL, and document.location.search contains the query string, e.g. ?foo=bar&spam=eggs. As for testing its presence, how about:

if(document.location.search.length) {
    // query string exists
} else {
    // no query string exists
}

No jQuery required :o

answered 2010-07-12T21:02:45.487
5

I would think you could use the javascript match operator.

var url = window.location.search;
if (url.match("your string").length > 0) {
}
answered 2010-07-12T21:03:05.140

Your Answer