How can I check if the query string contains a q=
in it using JavaScript or jQuery?
one more variant, but almost the same as Gumbos solution:
var isDebug = function(){
return window.location.href.search("[?&]debug=") != -1;
};
I've used this library before which does a pretty good job of what you're after. Specifically:-
qs.contains(name)
Returns true if the querystring has a parameter name, else false.
if (qs2.contains("name1")){ alert(qs2.get("name1"));}
This should help:
function getQueryParams(){
try{
url = window.location.href;
query_str = url.substr(url.indexOf('?')+1, url.length-1);
r_params = query_str.split('&');
params = {}
for( i in r_params){
param = r_params[i].split('=');
params[ param[0] ] = param[1];
}
return params;
}
catch(e){
return {};
}
}
You could also use a regular expression:
/[?&]q=/.test(location.search)