Is it possible using jquery (or just javascript) to check for the existence of a query string on the URL?
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 .
Check if a query string parameter existsThe URLSearchParams.has() method returns true if a parameter with a specified name exists.
The URLSearchParams interface defines utility methods to work with the query string of a URL.
Yes, the location
object has a property search
that contains the query string.
alert(window.location.search);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With