Javascript jquery - 检查 URL 上是否存在查询字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3232220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:46:32  来源:igfitidea点击:

jquery - check if query string present on URL?

javascriptjquery

提问by Probocop

Is it possible using jquery (or just javascript) to check for the existence of a query string on the URL?

是否可以使用 jquery(或仅使用 javascript)来检查 URL 上是否存在查询字符串?

回答by Felix Kling

Yes, the locationobject has a property searchthat contains the query string.

是的,该location对象有一个search包含查询字符串的属性。

alert(window.location.search);

回答by Matchu

document.locationcontains information about the URL, and document.location.searchcontains the query string, e.g. ?foo=bar&spam=eggs. As for testing its presence, how about:

document.location包含有关 URL 的信息,并document.location.search包含查询字符串,例如?foo=bar&spam=eggs. 至于测试它的存在,如何:

if(document.location.search.length) {
    // query string exists
} else {
    // no query string exists
}

No jQuery required :o

不需要 jQuery :o

回答by Neil Outler

I would think you could use the javascript match operator.

我认为您可以使用 javascript 匹配运算符。

var url = window.location.search;
if (url.match("your string").length > 0) {
}

回答by Rixius

Check out http://rixi.us/post/791257125/get-and-server-free-dynamic-contet

查看http://rixi.us/post/791257125/get-and-server-free-dynamic-contet

a method to extract any query string parameters
Use::

一种提取任何查询字符串参数的方法
使用::

if (_GET['key']) {
  var key = _GET['key']
}