How to check if a query string value is present via JavaScript?
You could also use a regular expression:
/[?&]q=/.test(location.search)
var field = 'q';
var url = window.location.href;
if(url.indexOf('?' + field + '=') != -1)
return true;
else if(url.indexOf('&' + field + '=') != -1)
return true;
return false
Using URL
:
url = new URL(window.location.href);
if (url.searchParams.get('test')) {
}
EDIT: if you're sad about compatibility, I'd highly suggest https://github.com/medialize/URI.js/.