Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Undefined is not a function on indexOf

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

like image 617
pattyd Avatar asked Jun 28 '14 21:06

pattyd


People also ask

How do I fix uncaught Typeerror e indexOf is not a function?

This is because indexOf was not supported in 3.3. 1/jquery. min. js so a simple fix to this is to change it to an old version 2.1.

What is indexOf in JavaScript?

The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.

Is not a function uncaught type error?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.

Why is my JavaScript function undefined?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .


1 Answers

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)
like image 88
Andbdrew Avatar answered Oct 14 '22 08:10

Andbdrew