How can I check if the query string contains a q= in it using JavaScript or jQuery?
q=
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
我以前使用过 这个图书馆,它在你所追求的目标方面做得很好。具体来说:-
qs.contains(name) Returns true if the querystring has a parameter name, else false. if (qs2.contains("name1")){ alert(qs2.get("name1"));}
您还可以使用正则表达式:
/[?&]q=/.test(location.search)
The plain javascript code sample which answers your question literally:
return location.search.indexOf('q=')>=0;
普通的 javascript 代码示例尝试查找 q 参数是否存在以及它是否有一个值:
var queryString=location.search; var params=queryString.substring(1).split('&'); for(var i=0; i<params.length; i++){ var pair=params[i].split('='); if(decodeURIComponent(pair[0])=='q' && pair[1]) return true; } return false;
还有一个变种,但几乎和 Gumbos 解决方案一样:
var isDebug = function(){ return window.location.href.search("[?&]debug=") != -1; };
这应该会有所帮助:
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 {}; } }
此函数帮助您从 JS 中的 URL 获取参数
function getQuery(q) { return (window.location.search.match(new RegExp('[?&]' + q + '=([^&]+)')) || [, null])[1]; }
试试这个
//field "search"; var pattern = /[?&]search=/; var URL = location.search; if(pattern.test(URL)) { alert("Found :)"); }else{ alert("Not found!"); }
JSFiddle: https://jsfiddle.net/codemirror/zj4qyao2/
Using URL:
URL
url = new URL(window.location.href); if (url.searchParams.has('test')) { }
编辑: 如果你对兼容性感到遗憾,我强烈建议 https://github.com/medialize/URI.js/。
编辑: 请参阅下面的评论和 Plabon 对为什么使用 get检查存在问题的回答。使用 searchParams.has()要好得多。
get
searchParams.has()
在现代浏览器中,由于使用了 URLSearchParams界面,这变得容易得多。这定义了许多实用工具方法来处理 URL 的查询字符串。
URLSearchParams
假设我们的 URL 是 https://example.com/?product=shirt&color=blue&newuser&size=m,您可以使用 window.location.search获取查询字符串:
https://example.com/?product=shirt&color=blue&newuser&size=m
window.location.search
const queryString = window.location.search; console.log(queryString); // ?product=shirt&color=blue&newuser&size=m
You can then parse the query string’s parameters using URLSearchParams:
const urlParams = new URLSearchParams(queryString);
然后可以在结果上调用它的任何方法。
例如,URLSearchParams.get()将返回与给定搜索参数关联的第一个值:
URLSearchParams.get()
const product = urlParams.get('product') console.log(product); // shirt const color = urlParams.get('color') console.log(color); // blue const newUser = urlParams.get('newuser') console.log(newUser); // empty string
可以使用 URLSearchParams.has()检查某个参数是否存在:
URLSearchParams.has()
console.log(urlParams.has('product')); // true console.log(urlParams.has('paymentmethod')); // false
如需进一步阅读,请按 给你。
更新已接受的答案,您可能只需返回 if 条件,而不是显式返回 true/false:
const field = 'q'; const url = window.location.href; return url.indexOf(`?${field}=`) !== -1 || url.indexOf(`&${field}=`) !== -1;
我对字符串进行了插值,但是你也可以把它们加在一起,就像公认的答案一样。