window.location.indexOf 在 Javascript 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13669472/
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
window.location.indexOf not working in Javascript
提问by Fahim Parkar
Below is what I have.
下面是我所拥有的。
var myString = "http://localhost:8888/www.smart-kw.com/";
alert(myString.indexOf("localhost"));
This give me alert... however if I change var myString = "http://localhost:8888/www.smart-kw.com/";to var myString = window.location;, it won't work (I don't get alert).
这给了我警报...但是,如果我更改var myString = "http://localhost:8888/www.smart-kw.com/";为var myString = window.location;,它将不起作用(我没有收到警报)。
var myString = window.location;
alert(myString.indexOf("localhost"));
回答by T.J. Crowder
window.locationis an accessor property, and getting its value gives you an object, not a string, and so it doesn't have an indexOffunction. (It's perfectly understandable that people sometimes think it's a string, since when you setits value, the accessor property's setter accepts a string; that is, window.location = "some url";actually works. But when you getit, you don't get a string.)
window.location是一个访问器属性,获取它的值会给你一个对象,而不是一个字符串,所以它没有indexOf函数。(人们有时认为它是一个字符串是完全可以理解的,因为当您设置它的值时,访问器属性的 setter 接受一个字符串;也就是说,window.location = "some url";实际上是有效的。但是当您得到它时,您并没有得到一个字符串。)
You can use window.location.toString(), String(window.location), or window.location.hrefto get a string for it if you like, or use any of its various propertiesto check specifics. From the link, given example url http://www.example.com:80/search?q=devmo#test:
如果您愿意,您可以使用window.location.toString()、String(window.location)或window.location.href为其获取字符串,或者使用其各种属性中的任何一个来检查细节。从链接中,给出示例 url http://www.example.com:80/search?q=devmo#test:
hash: The part of the URL that follows the # symbol, including the # symbol. You can listen for the hashchange event to get notified of changes to the hash in supporting browsers.
Example:#testhost: The host name and port number.
Example:www.example.com:80hostname: The host name (without the port number).
Example:www.example.comhref: The entire URL.
Example:http://www.example.com:80/search?q=devmo#testpathname: The path (relative to the host).
Example:/searchport: The port number of the URL.
Example:80protocol: The protocol of the URL.
Example:http:search: The part of the URL that follows the ? symbol, including the ? symbol.
Example:?q=devmo
hash: URL 中# 符号后面的部分,包括# 符号。您可以侦听 hashchange 事件以在支持的浏览器中获得有关哈希更改的通知。
例子:#testhost:主机名和端口号。
例子:www.example.com:80hostname:主机名(不带端口号)。
例子:www.example.comhref: 整个网址。
例子:http://www.example.com:80/search?q=devmo#testpathname: 路径(相对于主机)。
例子:/searchport: URL 的端口号。
例子:80protocol: URL 的协议。
例子:http:search: URL 后面的部分?符号,包括 ? 象征。
例子:?q=devmo
For instance, for your quoted example, you might check window.location.hostname === "localhost".
例如,对于您引用的示例,您可以检查window.location.hostname === "localhost".
回答by Alexander
As far as I know window.locationis a Location object.
据我所知window.location是一个Location object.
For instance, window.location.hrefwill give you the entire URL.
例如,window.location.href会给你整个 URL。
var url = window.location.href;
alert(url.indexOf("domain"));
But this kind of check is bound to trigger false-positives. You are better using window.location.hostnameproperty which holds the host name part.
但这种检查必然会引发误报。您最好使用window.location.hostname包含主机名部分的属性。
var hostname = window.location.hostname;
alert(hostname === "my.domain.com");
回答by Edward
I found a way to make this work:
我找到了一种方法来完成这项工作:
(window.location.href).indexOf("localhost") > -1)
I actually use this for my projects as conditionals and it works just fine.
我实际上在我的项目中使用它作为条件,它工作得很好。

