未捕获的TypeError:Undefined不是indexOf上的函数

pat*_*tyd 16 javascript get indexof typeerror location-href

我目前有这个代码来检查特定ID的网站URL GET选项,但是每当运行此代码时,我都会收到一个奇怪的错误: Uncaught TypeError: Undefined is not a function

这是我的代码:

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

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

</script>
Run Code Online (Sandbox Code Playgroud)

如果我能就这个问题得到一些指导,那将是非常棒的...我找不到使用该.indexOf()功能的类似问题

And*_*rew 21

window.location是一个Location对象,而不是一个字符串,indexOf是一个String(或Array)方法.

如果要搜索查询参数,请尝试

window.location.search.indexOf(familyId)
Run Code Online (Sandbox Code Playgroud)

或者如果你想检查整个网址,

window.location.toString().indexOf(familyId)
Run Code Online (Sandbox Code Playgroud)