Javascript javascript中的location.search是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14395090/
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
What is location.search in javascript
提问by jchand
I want to know what location.search.substring(1)actually does. I saw this code on some website. I tried to print using alert, but this did not give any result. Is it supposed to alert location href?
我想知道location.search.substring(1)实际上是做什么的。我在一些网站上看到了这段代码。我尝试使用 打印alert,但这没有给出任何结果。它应该提醒位置href吗?
alert(location.search.substring(1))
回答by sachleen
http://example.com/index.php?foo=bar
location.search
> ?foo=bar
location.search.substring(1)
> foo=bar
So that code will return the entire query parameters withoutthe question mark.
因此该代码将返回不带问号的整个查询参数。
回答by Gregor
The location.search property contains the query string of an URI (including the ?), if any.
location.search 属性包含 URI(包括 ?)的查询字符串(如果有)。
Example:
例子:
http://www.example.org/index.php?param=arg
location.search is ?param=arg
So your code snips away the leading ? and returns param=arg.
所以你的代码剪掉了领先的 ? 并返回param=arg。
回答by Tim S.
The search property returns the query portion of a URL, including the question mark (?).
search 属性返回 URL 的查询部分,包括问号 (?)。
That means, that location.search.substring(1)should return the data withoutthe question mark.
这意味着,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"
The "query porpotion" is the query string:
“查询部分”是查询字符串:
http://www.example.com/?property=value&property2=value
| query string |
回答by JuHwon
e.g. if you have the following url
例如,如果您有以下网址
http://www.example.org/index.htm?Browser=Netscape
then window.location.searchwill return ?Browser=Netscapeas a string
然后window.location.search将?Browser=Netscape作为字符串返回
回答by hoogw
Now is year of 2018, this is how you do it in 2018.
现在是 2018 年,这就是您在 2018 年的做法。
Sample URL:
示例网址:
http://localhost:10/mapserver1/viewer/?config=viewer_simple1&url=https://maps2.dcgis.dc.gov/dcgis/rest/services/Zoning/MapServer&zoom=17&lat=38.917292&long=-77.036420
My working code to extract each query parameter:
我提取每个查询参数的工作代码:
var ___zoom;
var ___lat;
var ___long;
var ___basemap;
var ___type;
var ___url;
var ___title;
var ___opacity;
if ( location.search.match(/zoom=([^&]*)/i) )
{
___zoom = location.search.match(/zoom=([^&]*)/i)[1];
}
if ( location.search.match(/lat=([^&]*)/i) )
{
___lat = location.search.match(/lat=([^&]*)/i)[1];
}
if (location.search.match(/long=([^&]*)/i))
{
___long = location.search.match(/long=([^&]*)/i)[1];
}
if (location.search.match(/basemap=([^&]*)/i))
{
___basemap = location.search.match(/basemap=([^&]*)/i)[1];
}
if (location.search.match(/type=([^&]*)/i))
{
___type = location.search.match(/type=([^&]*)/i)[1];
}
if (location.search.match(/url=([^&]*)/i))
{
___url = location.search.match(/url=([^&]*)/i)[1];
}
if (location.search.match(/title=([^&]*)/i))
{
___title = location.search.match(/title=([^&]*)/i)[1];
}
if (location.search.match(/opacity=([^&]*)/i))
{
___opacity = location.search.match(/opacity=([^&]*)/i)[1];
}
//console.log(location.search.match(/zoom=([^&]*)/i)[0]); // 'zoom=17'
//console.log(location.search.match(/zoom=([^&]*)/i)[1]); // '17'
console.log(___zoom);
console.log(___lat);
console.log(___long);
console.log(___basemap);
console.log(___type);
console.log(___url);
console.log(___title);
console.log(___opacity);
回答by Rehan
location.search returns a query string including the initial question mark. The substr method is to remove the initial question mark from the returned query string.
location.search 返回一个包含初始问号的查询字符串。substr 方法是从返回的查询字符串中去掉开头的问号。
The reason why you're not getting any results in the alert() is because you're trying to read query string on a website which doesn't have any.
在 alert() 中没有得到任何结果的原因是因为您试图在没有任何结果的网站上读取查询字符串。
Here's how you test:
以下是您的测试方式:
- Go to this URL
- Open up the console on your browser
- Paste the code snippet shared below and hit enter
- 转到此网址
- 在浏览器上打开控制台
- 粘贴下面共享的代码片段,然后按 Enter
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);
Old question but the answer might be helpful for new visitors on this page.
老问题,但答案可能对本页的新访问者有所帮助。
回答by Nick
It returns the query string, without the initial question mark. You'll only see a result if there's a query string on the page, e.g. http://www.example.com?parameter=value.
它返回查询字符串,不带初始问号。如果页面上有查询字符串,您只会看到结果,例如http://www.example.com?parameter=value。

