简体   繁体   English

动态创建的iframe的onload事件永远不会在IE中触发

[英]onload event for dynamically created iframe never fires in IE

I have an issue with IE where the iframe's "onload/load" events will not fire when I dynamically create the iframe, set its source to a pdf and append it to the document. 我有一个IE的问题,当动态创建iframe时,iframe的“onload / load”事件不会触发 ,将其源设置为pdf并将其附加到文档。

The reason I need to listen for the event is because I need to hide the iframe until the frame has loaded and then use some effects to fade it into view. 我需要监听事件的原因是因为我需要隐藏iframe直到帧加载,然后使用一些效果将其淡入视图。

I've got this to work in every browser I've tested in (Chrome, Firefox, Safari, mobile Safari), but it will not work in IE8->IE11. 我已经在我测试的每个浏览器(Chrome,Firefox,Safari,移动Safari)中都可以使用它,但它在IE8-> IE11中无效。

I've seen lots of posts about how IE doesn't fire the onload event, and from what I've gathered, the following should be working: 我已经看过很多关于IE如何不触发onload事件的帖子,而且根据我收集的内容,以下内容应该有效:

// listen for when the iframe's content has been loaded...
if (window.addEventListener)
    iframe.addEventListener("load", framedContentLoaded, false);
else if (window.attachEvent)
    iframe.attachEvent("onload", framedContentLoaded);
else
    iframe.onload = framedContentLoaded;

However, my function framedContentLoaded never gets fired in IE. 但是,我的函数framedContentLoaded永远不会在IE中被触发。

I've created a fiddle that reproduces the issue: http://jsfiddle.net/s5TUU/ 我创造了一个小提琴,重现了这个问题: http//jsfiddle.net/s5TUU/

I had the same issue, I was using browser detection and onreadystatechange event to cater for IE but then it stopped working for IE11. 我有同样的问题,我使用浏览器检测和onreadystatechange事件来迎合IE,但后来它停止了IE11的工作。

This bug has been reported and there is a workaround mentioned. 已报告此错误,并提到了一种解决方法。

The workaround is to put the pdf into an html page with an iframe and then load that html page into your primary iframe. 解决方法是将pdf放入带有iframe的html页面,然后将该html页面加载到主iframe中。 You will have nested iframes, it's not pretty but it seems to play well with IE. 你会有嵌套的iframe,它不漂亮,但它似乎与IE很好。 You will use an onload event which makes it a cross browser solution. 您将使用onload事件,使其成为跨浏览器解决方案。 You will need to make the parent and child iframes the same size and disable scrollbars on the parent iframe so you don't get double scrollbars. 您需要使父级和子级iframe具有相同的大小,并禁用父iframe上的滚动条,这样您就不会获得双滚动条。

In my case I was using ASP.Net MVC, I was setting my parent iframe src to call a controller action with the pdf url passed as a url parameter. 在我的情况下我使用ASP.Net MVC,我设置我的父iframe src来调用控制器动作,并将pdf url作为url参数传递。 The action was returning a view with the child iframe with the pdf assigned to its src. 该操作返回一个带有子iframe的视图,并将pdf分配给其src。

One easy solution would be to simply use Google Viewer for the PDF instead. 一个简单的解决方案是简单地使用Google Viewer for PDF。 Not only does this show the PDF, but the onLoad property will undoubtedly work. 这不仅显示了PDF,而且onLoad属性无疑也会起作用。 Google Viewer: https://docs.google.com/viewer I should also note that the proper way of embedding PDFs on a webpage is with the embed tag, not an iframe. Google Viewer: https//docs.google.com/viewer我还应该注意,在网页上嵌入PDF的正确方法是使用embed标记,而不是iframe。 Perhaps, you could try using onreadystatechange event instead? 也许,您可以尝试使用onreadystatechange事件吗?

Use jquery For this. 使用jquery为此。

Var iframe= $(""); Var iframe = $(“”); iframe.attr( .... ) iframe.attr(....)

$("#content").html(iframe); $( “#内容”)HTML(iframe中)。 iframe.load(myFunction); iframe.load(myFunction的);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM