为什么 IE8 不处理 iframe 加载事件?



示例代码:

<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
function on_iframe_load() {
    document.getElementById('iframe_a').onload = function() {
        alert('Thanks for the visit!');
    };
}
</script>
</head>
<body>
<iframe name="iframe_a" id="iframe_a"></iframe>
<a href="http://www.example.com/" target="iframe_a" onclick="on_iframe_load();">Go!</a>
</body>
</html>

它可以在所有主流浏览器中正常工作,但IE8(可能还有以前的版本)不理解它。

更新:刚刚想出了一个解决方案,但我不确定它是否正确编码。请查看:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script>
            var clicked = false;
            function activate() {
                clicked = true;
            }
            function pop() {
                if (clicked) {
                    alert('Thanks for the visit!');
                };
            }
        </script>
    </head>
    <body>
        <iframe name="iframe_a" onload="pop();"></iframe>
        <a href="http://www.example.com/" target="iframe_a" onclick="activate();">Go!</a>
    </body>
</html>

在 iframe 上使用内联属性似乎可以在 IE8 中解决此问题:

<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
function onIframeLoad(iframe) {
    if(iframe.src) {
        alert('Thanks for the visit!');
    }
}
function onLinkClick(url) {
    document.getElementById('iframe_a').src = url;
}
</script>
</head>
<body>
<iframe id="iframe_a" onload="onIframeLoad(this);"></iframe>
<a href="http://www.example.com/" onclick="onLinkClick(this); return false;">Go!</a>
</body>
</html>

按要求更新:

你应该尝试编写更不显眼的javascript。以这种方式编写代码可能会防止您在IE中出现这种奇怪的错误。

<!DOCTYPE html>
<html>
<body>
    <iframe id="display-frame"></iframe>
    <a href="http://www.example.com/">Go!</a>
    <script>
        window.onload = function() {
            var iframe = document.getElementById('display-frame'),
                link = document.getElementsByTagName('a')[0];
            // load handler
            function onIframeLoad() {
                alert('Thanks for the visit!');
            }
            // event handlers
            if(iframe.addEventListener) iframe.addEventListener('load', onIframeLoad, false);
            else if(iframe.attachEvent) iframe.attachEvent('onload', onIframeLoad);
            link.onclick = function() {
                iframe.src = this.href;
                return false;
            }
        };
    </script>
</body>
</html>

页面加载后,似乎无法使用 DOM 属性将加载侦听器添加到 IE 中的 iFrame。

但是你可以使用 attachEvent ,所以:

function on_iframe_load() {
    function foo() {
        alert('Thanks for the visit!');
    };
    var el = document.getElementById('iframe_a');
    if (el.attachEvent) {
      el.attachEvent('onload',foo);
    } else if (el.addEventListener) {
      el.addEventListener('load', 'foo', false);
    }
}

我在IE 6中进行测试并颠倒了通常的测试顺序,以便优先使用attachEvent而不是addEventListener。您可能希望测试较新版本的 IE,以查看相反的顺序是否有效,并测试其他类似 IE 的浏览器,例如 Opera。

编辑

测试后修改了代码(愚蠢的我)以使用addEventListener.以下是在IE和其他版本中工作的东西:

function on_iframe_load() {
    function foo() {
        alert('Thanks for the visit!');
    };
    var el = document.getElementById('iframe_a');
    if (el.attachEvent) {
      el.attachEvent('onload',foo);
    } else {
      el.onload = foo;
    }
}

如果在标记中使用 onload 属性,则无需使用脚本添加侦听器。

它适用于:)

在IE8,ff,chrome上进行测试

var iframe = document.getElementById('iframeid');
    if (iframe .attachEvent) {
      iframe .attachEvent('onload',alert("IE Iframe loaded"));
    } else {
      iframe .onload = alert("Other than IE Iframe loaded");
    }

只需使用 jquery:

$(iframe).bind( 'load', function(){} );

最新更新