Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

indexOf not working

var myurl = window.location;
    var pos = myurl.IndexOf("memberId");
    if (pos = -1) {
        alert("false");
    } else {
        alert("true");
     }

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?

like image 827
phil crowe Avatar asked Dec 15 '10 15:12

phil crowe


3 Answers

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:

Chrome console

like image 193
Felix Kling Avatar answered Nov 16 '22 03:11

Felix Kling


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)
like image 38
Jón Trausti Arason Avatar answered Nov 16 '22 04:11

Jón Trausti Arason


try var myurl = window.location.pathname;

like image 1
Stephen Avatar answered Nov 16 '22 04:11

Stephen