开发者

indexOf not working

var myurl = window.location;
    var pos = myurl.IndexOf("memberId");
    if (pos = -1) {
        alert("false");
    } else {
        alert("true");
开发者_JAVA技巧     }

For some reason I can't seem to get this simple method to work. Chrome says 'myurl does not contain the method 'indexOf''. Any reason?


Maybe typo but it should be

myurl.indexOf

lowercase i.

And location is an object, so you want:

var myurl = window.location.href;

(and all the other things people say in the comments and other answers ;))

Update: To see what kind of properties an object has, just type, in this case, window.location in the console:

indexOf not working


window.location returns an object. Perhaps you wanted window.location.pathname? :-)

There's also a problem with this line:

if (pos = -1)

It should be

if (pos == -1)


try var myurl = window.location.pathname;


var myurl = window.location.toString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜