在IE中检测具有PDF的iFrame的onload事件 [英] Detecting onload event in IE for iFrame which has PDF

查看:167
本文介绍了在IE中检测具有PDF的iFrame的onload事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这个问题在这里被多次询问过。一些解决方案部分有效。这就是场景。我需要使用< embed> 加载一个包含pdf的iframe。有可能找不到PDF,我将在iFrame中显示错误页面。

I see that this question has been asked many times here. Some of the solutions partially work. This is the scenario. I need to load an iframe which has a pdf using <embed>. There are chances the PDF is not found and I will be showing error page within iFrame.

加载iframe后,我会使用

Once the iframe is loaded, I look for <embed> tag using

$('#iframe')。contents()。find('embed')

我尝试使用 $('#iframe')。load()。这适用于Firefox,Chrome但不适用于IE。

I tried using $('#iframe').load(). This works in Firefox, Chrome but not IE.

还尝试使用 $('#iframe')。ready()。这适用于IE,但是,

Also tried using $('#iframe').ready(). This works in IE, however,

$('#iframe')。contents()。find('embed')不起作用。

有人可以帮忙吗?

编辑: IE版本:7或8.尊重相同的原产地政策。

EDIT : IE Version : 7 or 8. Same origin policy is respected.

推荐答案

$('#iframe')。load < iframe onload => 不起作用。在IE浏览器中看起来无法访问PDF的iframe内容。因此,要跟踪iFrame加载,可以使用以下代码。

$('#iframe').load or <iframe onload=""> doesn't work. Looks like in IE iframe contents for PDF cannot be accessed. So to track iFrame load, following code can be used.

$(window).load(function() { 
    try {
            var s = window.frames[ "iframeName" ].document.body.childNodes[0].innerHTML;
            if(s.indexOf('My error message') > 0) {
                alert('Error');
            } else {
                alert('PDF');
            }
        } catch (err) {
            if($.trim(err.message) != 'Access is denied.') {
                alert('PDF');
            } else {
                alert('Error');
            }
        }
});

因此,如果发现错误消息我的错误消息,那么它是自定义设计的 - 有错误的错误页面,否则是PDF。

So in case a error message 'My error message' is found then it is the custom-designed-error page which has the message, else it is the PDF.

上述解决方案适用于IE7& IE8。对于其他浏览器,可以检查以下内容:
$('#iframe')。contents()。find('embed')。attr('src')

The above solution is for IE7 & IE8. For the rest of the browsers, following can be checked $('#iframe').contents().find('embed').attr('src')

不是一个直接的解决方案。但对我有用。

Not a straight forward solution. But works for me.

这篇关于在IE中检测具有PDF的iFrame的onload事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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