Uncaught TypeError: Undefined is not a function on indexOf

人走茶凉 提交于 2019-12-03 15:44:30

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/24471229/uncaught-typeerror-undefined-is-not-a-function-on-indexof

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!