Uncaught TypeError:未定义不是indexOf上的函数 [英] Uncaught TypeError: Undefined is not a function on indexOf

查看:375
本文介绍了Uncaught TypeError:未定义不是indexOf上的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有这个代码来检查特定ID的网址URL GET选项,但是无论何时运行此代码,我都会得到一个奇怪的错误: Uncaught TypeError:未定义不是函数

这是我的代码:

 < 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()函数

解决方案

window.location是 位置 对象,而不是字符串,并且 indexOf String (或 Array )方法。



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

  window.location.search.indexOf(familyId)

或者如果你想检查整个网址,那么你可以使用

$ p $ window.location.toString()。indexOf( familyId)


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

解决方案

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)

这篇关于Uncaught TypeError:未定义不是indexOf上的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆