Probocop
Probocop

Reputation: 10552

jquery - check if query string present on URL?

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

Upvotes: 28

Views: 54299

Answers (4)

Rixius
Rixius

Reputation: 2303

Check out http://rixi.us/post/791257125/get-and-server-free-dynamic-contet

a method to extract any query string parameters
Use::

if (_GET['key']) {
  var key = _GET['key']
}

Upvotes: -3

Neil Outler
Neil Outler

Reputation: 274

I would think you could use the javascript match operator.

var url = window.location.search;
if (url.match("your string").length > 0) {
}

Upvotes: 5

Matchu
Matchu

Reputation: 85862

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

Upvotes: 32

Felix Kling
Felix Kling

Reputation: 817128

Yes, the location object has a property search that contains the query string.

alert(window.location.search);

Upvotes: 40

Related Questions