Uncaught TypeError: Undefined is not a function on indexOf
Asked Answered
E

1

16

I currently have this code to check the website URL GET options for a specific ID, but whenever this code is run, I get a weird error: Uncaught TypeError: Undefined is not a function

Here is my code:

<script language="JavaScript">
    var familyid = "id=8978566";
    var corporateid = "id=8978565";

    if(window.location.indexOf(familyid) === -1)
       {
        document.write("Family ID not found");
       }

</script>

It would be awesome if i could get some guidance on this issue... I couldn't find similar issues using the .indexOf() function

Examinant answered 28/6, 2014 at 21:41 Comment(0)
B
21

window.location is a Location object, not a string, and indexOf is a String (or Array) method.

If you want to search the query params, try

window.location.search.indexOf(familyId)

or if you want check the whole url,

window.location.toString().indexOf(familyId)
Benuecongo answered 28/6, 2014 at 21:45 Comment(1)
You could try calling a toString. Making that one line: if(window.location.toString().indexOf(familyid) === -1)Yuma

© 2022 - 2024 — McMap. All rights reserved.