IndexOf not working

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

For some reason, I cannot get this simple method to work. Chrome says: “myurl does not contain the“ indexOf ”method. Any reason?

+5
source share
4 answers

Maybe a typo, but it should be

myurl.indexOf

lowercase i.

And locationis an object , so you want:

var myurl = window.location.href;

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

Update: To find out what properties an object has, just enter in this case window.locationin the console:

Chrome console

+9
source

window.locationreturns an object. Perhaps you would like window.location.pathname? :-)

There is also a problem with this line:

if (pos = -1)

if (pos == -1)
+3

var myurl = window.location.pathname;

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