Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 520
Probocop Avatar asked Jul 12 '10 20:07

Probocop


People also ask

How do I know if a URL contains a query string?

To check if a url has query parameters, call the indexOf() method on the url, passing it a question mark, and check if the result is not equal to -1 , e.g. url. indexOf('? ') !== -1 .

How do I check if a query string parameter exists?

Check if a query string parameter existsThe URLSearchParams.has() method returns true if a parameter with a specified name exists.

What is URLSearchParams?

The URLSearchParams interface defines utility methods to work with the query string of a URL.


2 Answers

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

alert(window.location.search); 
like image 100
Felix Kling Avatar answered Sep 25 '22 10:09

Felix Kling


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

like image 43
Matchu Avatar answered Sep 24 '22 10:09

Matchu