IE提示JavaScript函数未定义,但在Chrome中运行正常

4

我有一个网页包含一个链接 - 点击链接可以打开一个新窗口显示YouTube视频。在Chrome中可以正常工作,但在IE中无法运行。IE控制台显示:SCRIPT5009:'open_win'未定义(我正在使用IE11)。

在标签中我有:

function open_win(url)
   {
   new_window = open('','video','width=500,height=390,menubar=no,status=no,location=no,toolbar=no,scrollbars=yes');
   // open new document 
    new_window.document.open();
    // Text of the new document 
    new_window.document.write("<html><head></head><body style='background-color:'black';margin-top:0; margin-right:0; margin-bottom:0; margin-left:0;'>");
    new_window.document.write("<iframe width='560' height='315' src='https://www.youtube.com/embed/-linkRedacted-' frameborder='0' allow='autoplay; encrypted-media' allowfullscreen></iframe>");
    new_window.document.write("</body></html>");
    // close the document
    new_window.document.close(); 
    }

我现在有以下内容:

<a class="audio" href="javascript:void(0)" onclick="open_win()">
<img alt="See a demo video (opens in New window)" src="images/demo.jpg" width="120" height="110" /></a>

这是一个IE安全区域问题,还是编码错误?

太棒了 - 你有答案 - 谢谢!不知道为什么它没有被找到(这肯定是这样的函数应该存在的地方吧?) - user801347
发布的解决方案似乎已经消失了 ;? 它是将函数从头部移动到正文部分。 - user801347
代码是否在<script>...</script>标签内? - Barmar
嗯,@Barmar - 这个解决方案(似乎已经消失了?)说IE11在头部区域找不到函数。所以我把它移到了<body>中,现在它可以工作了。而且,很高兴地告诉你,是在<script>标签内的。 - user801347
它“消失了”,因为他删除了它,可能是因为他意识到它不正确。 - Barmar
显示剩余4条评论
1个回答

0

两件事情:

  • 你的javascript代码中是否使用了strict模式?如果是,可能是因为你调用函数时没有传递函数变量,导致IE11不兼容。
  • 你的javascript代码是否在一个单独的文件中?可以尝试将函数复制到html代码上方,看看是否有所不同。

好的想法,但是我有:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">。我已经尝试过传递变量和不传递变量。 - user801347
而且JavaScript位于页面的head部分。 - user801347
1
你最终是通过将JavaScript从头部移除来解决这个问题的吗? - pg316
我尝试将JS放置在页面底部的头部区域,这在IE11中有效,而我之前遇到了问题。谢谢。 - vsoni

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