我想知道究竟location.search.substring(1)做了什么.我在某个网站上看到了这段代码.我尝试使用打印alert,但这没有给出任何结果.它应该提醒位置href吗?
alert(location.search.substring(1))
Run Code Online (Sandbox Code Playgroud)
sac*_*een 46
http://example.com/index.php?foo=bar
location.search
> ?foo=bar
location.search.substring(1)
> foo=bar
Run Code Online (Sandbox Code Playgroud)
因此,代码将返回没有问号的整个查询参数.
Gre*_*gor 17
location.search属性包含URI的查询字符串(包括?),如果有的话.
例:
http://www.example.org/index.php?param=arg
location.search is ?param=arg
Run Code Online (Sandbox Code Playgroud)
所以你的代码会削减领先优势?并返回param=arg.
Tim*_* S. 13
search属性返回URL的查询部分,包括问号(?).
这意味着,location.search.substring(1)应该返回没有问号的数据.
// http://www.example.com/index.html
console.log(location.search.substring(1)); // no query string, so displays nothing
// http://www.example.com/index.html?property=value
console.log(location.search.substring(1)); // should display "property=value"
Run Code Online (Sandbox Code Playgroud)
"查询porpotion"是查询字符串:
http://www.example.com/?property=value&property2=value
| query string |
Run Code Online (Sandbox Code Playgroud)
例如,如果你有以下网址
http://www.example.org/index.htm?Browser=Netscape
Run Code Online (Sandbox Code Playgroud)
然后window.location.search将?Browser=Netscape作为字符串返回
location.search 返回一个包含初始问号的查询字符串。substr 方法是从返回的查询字符串中去掉开头的问号。
在 alert() 中没有得到任何结果的原因是因为您试图在没有任何结果的网站上读取查询字符串。
以下是您的测试方式:
var container = {};
location.search.split('&').toString().substr(1).split(",").forEach(item => {
container[item.split("=")[0]] = decodeURIComponent(item.split("=")[1]) ? item.split("=")[1]: "No query strings available" ;
});
console.log(container);Run Code Online (Sandbox Code Playgroud)
老问题,但答案可能对本页的新访问者有所帮助。
| 归档时间: |
|
| 查看次数: |
74978 次 |
| 最近记录: |