为什么这个部分会返回一个结果:
var s = "foo";
alert(s.indexOf("oo") > -1);
但这部分没有:
var currentLocation = window.location;
alert(currentLocation.indexOf(".") > -1);
因为window.locaton
不是字符串。它是一个具有多个属性的对象。
您可以检查整个URL的window.location.href
(保证包含一个点),或者只检查路径的window.location.pathname
(在域之后)。
window.location.toString()
var currentLocation = window.location.toString();
alert(currentLocation.indexOf("."));
演示
查看所有属性
showLoc();
function showLoc() {
var oLocation = window.location, aLog = ["Property (Typeof): Value", "window.location (" + (typeof oLocation) + "): " + oLocation ];
for (var sProp in oLocation){
aLog.push(sProp + " (" + (typeof oLocation[sProp]) + "): " + (oLocation[sProp] || "n/a"));
}
alert(aLog.join("n"));
}
DEMO