通过 JavaScript 动态设置的 iframe src 在 Internet Explorer 上执行两次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3007764/
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 02:53:07  来源:igfitidea点击:

Iframe src set dynamically through JavaScript is being executed twice on Internet Explorer

javascriptinternet-explorerdomiframe

提问by Pierre Grima

I am encountering a very annoying problem with IE. Basically I need to set the source of an IFrame using JavaScript, however the source document is being executed twice not once during each call.

我遇到了一个非常烦人的 IE 问题。基本上我需要使用 JavaScript 设置 IFrame 的源,但是在每次调用期间源文档被执行两次而不是一次。

The simplified HTML code is pasted below (I simplified it so that readers can understand it quickly. The source is being set through JavaScript since it will contain dynamic content):

简化后的HTML代码贴在下面(我把它简化了,以便读者可以快速理解。源代码是通过JavaScript设置的,因为它会包含动态内容):

<html>
<head>
<title>Iframe test</title>
</head>
<body>

<iframe id="testIframe" scrolling="no" frameborder="0" src="" width="800" height="600"></iframe>

<script language="JavaScript">
    document.getElementById("testIframe").src = "http://localhost/test.php";
</script>

</body>
</html>

In this example, test.php inserts a record inside a local database once called. Whenever the page above is called using IE, two rows are being inserted on most occasions (sometimes only 1 row is inserted but this is not the norm). I tested the same script on Chrome and Opera and it works correctly on them so this must be an IE issue.

在这个例子中,test.php 会在调用后在本地数据库中插入一条记录。每当使用 IE 调用上面的页面时,大多数情况下都会插入两行(有时只插入 1 行,但这不是常态)。我在 Chrome 和 Opera 上测试了相同的脚本,它在它们上正常工作,所以这一定是 IE 问题。

If is set the src directly inside the iframe tag IE starts behaving correctly, however I need to be able to build the URL using javascript.

如果直接在 iframe 标签内设置 src,IE 将开始正常运行,但是我需要能够使用 javascript 构建 URL。

Has anyone encountered this issue? and does anyone know of a solution/workaround?

有没有人遇到过这个问题?有谁知道解决方案/解决方法?

Thanks and Regards Pierre

感谢和问候皮埃尔

回答by Pierre Grima

I managed to solve the issue. Basically I am now using

我设法解决了这个问题。基本上我现在正在使用

window.frames['testIframe'].document.location.href

instead of

代替

document.getElementById("testIframe").src

and the source document is being hit only once.

并且源文档只被命中一次。

回答by Allie

I also encountered this same problem, but in my case the cause was IE10. The src that I set dynamically to the IFRAME was opened twice. I resolved the issue by including this in the section :

我也遇到了同样的问题,但就我而言,原因是 IE10。我动态设置为 IFRAME 的 src 被打开了两次。我通过将其包含在以下部分中解决了该问题:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9;">

回答by Odd-Arne

Put the script in the

将脚本放入

<head> </head>

not in the

不在

<body></body>

The reason to this is as follows, it will run the iframe once and draw this up, then because the script follows, it will run it again. Had same issue, dropped iframe, due to the demand from search engines want you not to use iframe.

这样做的原因如下,它会运行一次 iframe 并将其绘制出来,然后因为脚本如下,它会再次运行它。有同样的问题,丢弃了 iframe,因为搜索引擎要求你不要使用 iframe。