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 = window.location;
alert(myString.indexOf("localhost"));
window.locationis an accessor property, and getting its value gives you an object, not a string, and so it doesn’t have anindexOffunction. (It’s perfectly understandable that people sometimes think it’s a string, since when you set its value, the accessor property’s setter accepts a string; that is,window.location = "some url";actually works. But when you get it, you don’t get a string.)You can use
window.location.toString(),String(window.location), orwindow.location.hrefto get a string for it if you like, or use any of its various properties to check specifics. From the link, given example urlhttp://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=devmoFor instance, for your quoted example, you might check
window.location.hostname === "localhost".