在Internet Explorer中JavaScript无法运行

3

我写了一些JavaScript代码,用于在用户滚动时移动广告横幅。然而,除了Internet Explorer浏览器外,它在其他浏览器中都能正常工作...似乎我的JS甚至没有开始运行...

我正在使用最新的IE9。

我是这样调用它的:

<html>
<head>
  <script src="./js/move-it.js" type="text/javascript"></script>
</head>
<body>
  ...
  <div id="bird">
    <iframe ...>...</iframe>
  </div>
  <script type="text/javascript">
    start(); // method in move-it.js
  </script>
</body>

网站: http://lolkitten.org

顺便问一下,有人知道如何以一种好的方式防止横幅广告与页脚碰撞吗?我尝试在较低的广告上方放置一个名为“stopper”的div,并使用它的“offset-top”属性,但我想它总是给我一个太小的值,即它仍然会碰撞...-.-

谢谢


我在http://lolkitten.org/wp-content/themes/lolkitten/js/move-it.js中找不到名为start()的函数。 - Matt Gibson
@Matt 但是,在其他网络浏览器中仍在运行。 - user577898
@SachinShekhar 是的,这表明要么它由于start()而没有移动,要么是因为start()在另一个文件中。至少,发帖人对这个函数感到困惑。 - Matt Gibson
@Cedric 你能澄清一下 start() 应该在哪里,并且发布其中的代码吗? - Matt Gibson
哦,我忘记我已经重命名它了,在做我的玩具示例时没有太多思考... 现在它叫做 launchBird(),并且直接放在包含广告的 div#bird 后面。 对此感到抱歉... - Cedric Reichenbach
4个回答

14

我现在已经成功让它工作了。 问题在于IE不允许Javascript中使用const修饰符。我将它们简单地改为var,这样它就可以很好地运行了。


3
为什么会有踩票?IE浏览器(至少版本<=9)不支持const关键字。你可以试一下,如果你打算踩我,请留下评论! :/ - Cedric Reichenbach
9
我不在乎那些虚拟的“-2分”,只是因为点踩意味着有人认为回答是错误的,所以他们应该解释一下为什么,来让我们其他人受益。我仍然相信上面的陈述是正确的,但如果不是,我非常想知道为什么,并了解细节。 - Cedric Reichenbach

6

IE所有版本都不容忍PHP程序员常见的错误。以下是IE不接受的Javascript语法列表,适用于所有函数,包括AJAX:

(这些IE Javascript语法错误在Opera、Chrome和Firefox中并不是错误。)

  1. you cannot set a default variable value in function declaration parameters function thisFunction(something='x'){ thingy.here;} is not allowed and will read as an undeclared function when thisFunction() is called.

  2. Passing objects as function parameters can have unexpected results: function(someObject) may or may not work depending on the context.

  3. undeclared variables stop the script

  4. event.preventDefault(); cannot be called inside the called function, and will stop the script

  5. event.preventDefault(); must be declared first, in the event reference before all other functions... this is not true on other browsers. So IE must operate asynchronously by default...

    May not be expected when PHP programmers first learn to enjoy the synchronous character of Javascript as lesson #1 in the language. This example does not work (does not work) in the 'a' tag when put directly into the tag of a link

    onclick="function() { 
        if(typeof someFunction === 'function') { 
            event.preventDefault(); 
            someFunction('anyParameter'); 
        };"
    

    When you are using the same code on a page that doesn't implement or declare the function someFunction(). what a waste of time!

  6. onclick='clickChild(this);' doesn't work on IE... and I don't know why. It will actually stop a contained link and nothing will happen. Seems to contradict the above behavior of preventDefault which only works as the first function in an event reference, and if not called first a containing link will be followed. So inconsistent logic in IE. ---as of today, January 21, 2017, in today's Google Chrome update, the above function also does not work in Chrome.

在表单中,'button'标签在表单提交时不能正确返回值。新版的IE可能会修复这个问题,也可能不会。对于我的当前项目:yad1.org,'button'标签是必需的,因为需要在多种语言中使用相同的值来提交按钮名称。
结论:IE应该从Windows中移除并永远被遗忘。
真正浪费时间调试。IE需要接触程序员友好的JavaScript世界。

可以确认在IE 11中1仍然是正确的。真是个笑话。 - Spartacus

0
相当长的时间后...在客户机器上使用IE11出现了同样的问题(Firefox和Edge正常工作)。文件位于(UNC)

file://///server/share/directory/index.html

已正确加载,但JavaScript未激活。

在这种情况下的解决方案:

  1. "Internet选项" > "安全"选项卡 > 区域"本地 Intranet" > 按钮"Sites" > 取消第三个选项"包括所有网络路径(UNCs)" (需要重新加载页面)
  2. "Internet选项" > "安全"选项卡 > 区域"受信任的站点" > 按钮"Sites" > 将"file://server"添加到网站列表中 (需要重新启动IE)

区域本地 Intranet

Zone Local intranet

现在它可以工作了。


0
  1. 在“工具”菜单上,点击“Internet选项”,然后点击“安全”选项卡。
  2. 点击“Internet区域”。
  3. 如果您不需要自定义Internet安全设置,请点击“默认级别”。然后执行第4步。
    如果您需要自定义Internet安全设置,请按照以下步骤操作:

    a. 点击“自定义级别”。
    b. 在“安全设置-Internet区域”对话框中,在“脚本”部分中点击“启用Active Scripting”。

  4. 点击“返回”按钮返回到上一页,然后点击“刷新”按钮运行脚本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接