IE 抛出 JavaScript 错误:属性 'googleMapsQuery' 的值为 null 或未定义,不是函数对象(适用于其他浏览器)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10128973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:59:50  来源:igfitidea点击:

IE throws JavaScript Error: The value of the property 'googleMapsQuery' is null or undefined, not a Function object (works in other browsers)

javascriptfunctioninternet-explorer-9undefinedruntime-error

提问by chriv

I'm having a real problem with JavaScript scope in IE 9.

我在 IE 9 中遇到了 JavaScript 范围的真正问题。

This is inside the body of my document (yes, I realize script should go in the head for proper HTML, but I've never had it break a script before). The script is in the body because I don't want to mess with a shared header page for a script that is only relevant for this page:

这是在我的文档正文中(是的,我意识到脚本应该放在正确的 HTML 中,但我以前从未让它破坏过脚本)。该脚本在正文中,因为我不想弄乱一个仅与此页面相关的脚本的共享标题页面:

<script type="text/javascript">
function googleMapsQuery(accountNum) {
    // function code is here (omitted for brevity)
}
</script>

This is inside a tdblock inside a trblock inside a tbodyblock inside a tableblock inside a formblock inside the body:

这是在td块内的tr块内的tbody块内的table块内的form块内的块内的块内的块内body

<button id="google-422111" onclick="googleMapsQuery(422111)" type="button">Google This!</button>

I even moved the script block above the form just in case the order of the script function declaration was relevant (it's not).

我什至将脚本块移到表单上方,以防脚本函数声明的顺序相关(不是)。

The script works flawlessly in FireFox and Chrome, but in IE 9 (with or without compatibility view on), I get this error:

该脚本在 FireFox 和 Chrome 中完美运行,但在 IE 9(打开或不打开兼容性视图)中,我收到此错误:

SCRIPT5007: The value of the property 'googleMapsQuery' is null or undefined, not a Function object

SCRIPT5007:属性“googleMapsQuery”的值为 null 或未定义,不是 Function 对象

I studied JavaScript scope, and I cannot figure out any reason why IE thinks that 'googleMapsQuery' is a property, and why it is undefined. It's a function, and I defined it!

我研究了 JavaScript 范围,但我无法弄清楚为什么 IE 认为 'googleMapsQuery' 是一个属性,以及为什么它是未定义的。这是一个函数,我定义了它!

采纳答案by chriv

I found the answer, and in spite of what I reported, it was NOT browser specific. The bug was in my function code, and would have occurred in any browser. It boils down to this. I had two lines in my code that were FireFox/FireBug specific. They used console.log. In IE, they threw an error, so I commented them out (or so I thought). I did a crappy job commenting them out, and broke the bracketing in my function.

我找到了答案,尽管我报告了什么,但它不是特定于浏览器的。该错误在我的函数代码中,并且会在任何浏览器中发生。归结为这一点。我的代码中有两行是特定于 FireFox/FireBug 的。他们使用了console.log。在 IE 中,他们抛出了一个错误,所以我将它们注释掉了(或者我是这么认为的)。我做了一个糟糕的工作来评论它们,并打破了我函数中的括号。

Original Code (with console.log in it):

原始代码(包含 console.log):

if (sxti.length <= 50) console.log('sxti=' + sxti);
if (sxph.length <= 50) console.log('sxph=' + sxph);

Broken Code (misplaced brackets inside comments):

损坏的代码(注释中括号错位):

if (sxti.length <= 50) { //console.log('sxti=' + sxti); }
if (sxph.length <= 50) { //console.log('sxph=' + sxph); }

Fixed Code (fixed brackets outside comments):

固定代码(注释外的固定括号):

if (sxti.length <= 50) { }//console.log('sxti=' + sxti);
if (sxph.length <= 50) { }//console.log('sxph=' + sxph);

So, it was my own sloppy coding. The function really wasn't defined, because a syntax error kept it from being closed.

所以,这是我自己草率的编码。该函数确实没有定义,因为语法错误使其无法关闭。

Oh well, live and learn. ;)

哦,生活和学习。;)

回答by Chris

I was having a similar issue with a property being null or undefined.

我遇到了类似的问题,属性为空或未定义。

This ended up being that IE's document mode was being defaulted to IE7 Standards. This was due to the compatibility mode being automatically set to be used for all intranet sites (Tools> Compatibility View Setting> Display Intranet Sites in Compatibility View).

这最终导致 IE 的文档模式被默认为 IE7 标准。这是因为兼容模式被自动设置为用于所有 Intranet 站点(工具>兼容性视图设置>在兼容性视图中显示 Intranet 站点)。

回答by Mark

Have you tried adding the semicolon to onclick="googleMapsQuery(422111);". I don't have enough of your code to test if the missing semicolon would cause the error, but ie is more picky about syntax.

您是否尝试将分号添加到onclick="googleMapsQuery(422111);". 我没有足够的代码来测试缺少的分号是否会导致错误,但是 ie 对语法更加挑剔。

回答by Coyte

So I had a similar situation with the same error. I forgot I changed the compatibility mode on my dev machine and I had a console.log command in my javascript as well. I changed compatibility mode back in IE, and removed the console.log command. No more issue.

所以我遇到了类似的情况,也有同样的错误。我忘了我在我的开发机器上更改了兼容模式,我的 javascript 中也有一个 console.log 命令。我在 IE 中更改了兼容模式,并删除了 console.log 命令。没有更多的问题。

回答by TrinTragula

In my particular case, I had a similar error on a legacy website used in my organization. To solve the issue, I had to liist the website a a "Trusted site".

在我的特殊情况下,我在我的组织中使用的旧网站上遇到了类似的错误。为了解决这个问题,我不得不将网站列为“受信任的站点”。

To do so:

这样做:

  • Click the Toolsbutton, and then Internet options.
  • Go on the Securitytab.
  • Click on Trusted sitesand then on the sites button.
  • Enter the url of the website and click on Add.
  • 单击工具按钮,然后单击Internet 选项。
  • 转到“安全”选项卡。
  • 单击受信任的站点,然后单击站点按钮。
  • 输入网站的 url,然后单击Add

I'm leaving this here in the remote case it will help someone.

我把这个放在远程案例中,它会帮助某人。

回答by user2384794

Just a few minutes ago i was facing the same problem. I got the problem that is after just placing your jQuery start the other jQuery scripting. After all it will work fine.

就在几分钟前,我面临着同样的问题。我遇到的问题是在放置 jQuery 后启动其他 jQuery 脚本。毕竟它会正常工作。