【问题标题】:Uncaught TypeError: Undefined is not a function on indexOf未捕获的类型错误:未定义不是 indexOf 上的函数
【发布时间】:2014-08-19 17:01:16
【问题描述】:

我目前有此代码来检查特定 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>

如果我能在这个问题上获得一些指导,那就太棒了......我使用 .indexOf() 函数找不到类似的问题

【问题讨论】:

    标签: javascript get typeerror indexof location-href


    【解决方案1】:

    window.location 是 Location 对象,而不是字符串,indexOfString(或 Array)方法。

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

    window.location.search.indexOf(familyId)
    

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

    window.location.toString().indexOf(familyId)
    

    【讨论】:

    • 非常感谢!这解决了它!
    • 您可以尝试调用 toString。制作那一行: if(window.location.toString().indexOf(familyid) === -1)
    猜你喜欢
    • 1970-01-01
    • 2015-03-14
    • 2014-12-13
    • 2014-05-25
    相关资源
    最近更新 更多