动态创建的iframe的onload事件永远不会在IE中触发 [英] onload event for dynamically created iframe never fires in IE

查看:831
本文介绍了动态创建的iframe的onload事件永远不会在IE中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IE中遇到问题,当动态创建iframe时,iframe的onload / load事件将不会触发,将其源设置为pdf并将其附加到文档中。

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.

我需要监听事件的原因是因为我需要隐藏iframe直到帧加载,然后使用一些效果将其淡入视图。

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.

我已经在我测试的每个浏览器(Chrome,Firefox,Safari,移动版Safari)中都能使用它,但它在IE8-> IE11中无效。

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.

我看过很多关于IE如何不触发onload事件的帖子,而且根据我收集的内容,以下内容应该有效:

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:

// 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;

然而,我的函数 framedContentLoaded 永远不会被解雇在IE浏览器中。

However, my function framedContentLoaded never gets fired in IE.

我创造了一个再现问题的小提琴: http://jsfiddle.net/s5TUU/

I've created a fiddle that reproduces the issue: http://jsfiddle.net/s5TUU/

推荐答案

我遇到了同样的问题,我使用的是浏览器检测并 onreadystatechange 事件以满足IE的要求,但它已停止为IE11工作。

I had the same issue, I was using browser detection and onreadystatechange event to cater for IE but then it stopped working for IE11.

已报告此错误并且提到了一种解决方法。

This bug has been reported and there is a workaround mentioned.

解决方法是将pdf放入带有iframe的html页面,然后将该html页面加载到主iframe中。你会有嵌套的iframe,它不漂亮,但它似乎与IE很好。您将使用 onload 事件,这使其成为跨浏览器解决方案。
您需要使父级和子级iframe的大小相同,并禁用父iframe上的滚动条,这样您就不会获得双滚动条。

The workaround is to put the pdf into an html page with an iframe and then load that html page into your primary iframe. You will have nested iframes, it's not pretty but it seems to play well with IE. You will use an onload event which makes it a cross browser solution. 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.

在我的我正在使用ASP.Net MVC,我设置我的父iframe src来调用控制器动作,并将pdf url作为url参数传递。该操作返回了一个包含子iframe的视图,其中pdf已分配给其src。

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. The action was returning a view with the child iframe with the pdf assigned to its src.

这篇关于动态创建的iframe的onload事件永远不会在IE中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆