2010-07-12 106 views

回答

34

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

alert(window.location.search); 
25

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

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

没有jQuery的要求:○

4

我想你可以使用JavaScript匹配运算符。

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

fyi,这是不好的,因为匹配可以返回空值,并且放置空对象的长度会导致错误 – sksallaj 2012-08-17 21:12:02

-3

退房http://rixi.us/post/791257125/get-and-server-free-dynamic-contet

提取任何查询字符串参数
使用::

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

-1。 OP指定jQuery(或Javascript)不是PHP作为他们正在使用的语言。 – theringostarrs 2014-03-23 21:24:51

+0

@theringostarrs它实际上不是PHP,它只是我写了很久很久以前的一小段JavaScript,它允许您从URL中提取GET变量。完全过时了,我不确定这个链接是否可以工作,但它是/是Javascript。 – Rixius 2014-03-27 21:36:05