Javascript 为什么';IE8是否处理iframe onload事件?

Javascript 为什么';IE8是否处理iframe onload事件?,javascript,html,iframe,internet-explorer-8,Javascript,Html,Iframe,Internet Explorer 8,示例代码: <!DOCTYPE html> <html> <head> <title></title> <script> function on_iframe_load() { document.getElementById('iframe_a').onload = function() { alert('Thanks for the visit!'); }; } </script>

示例代码:

<!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>

_iframe_load()上的函数{
document.getElementById('iframe_a')。onload=function(){
警惕(“感谢您的访问!”);
};
}
它在所有主流浏览器中都可以正常工作,但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>

var=false;
函数激活(){
单击=真;
}
函数pop(){
如果(单击){
警惕(“感谢您的访问!”);
};
}

页面加载后,似乎无法使用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);
    }
}
我在IE6中进行测试,并颠倒了通常的测试顺序,以便使用
attachEvent
优先于
addEventListener
。你可能想测试IE的最新版本,看看相反的顺序是否有效,还可以测试其他IE浏览器,比如Opera

编辑 将测试后的代码(傻瓜me)修改为使用
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属性,则不需要使用脚本添加侦听器。

在iframe上使用inline属性似乎可以解决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>

函数onIframeLoad(iframe){
if(iframe.src){
警惕(“感谢您的访问!”);
}
}
函数onLinkClick(url){
document.getElementById('iframe_a')。src=url;
}
按请求更新:

您应该尝试编写更不引人注目的javascript。以这种方式编写代码可以防止IE中出现这种奇怪的bug

<!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>

window.onload=函数(){
var iframe=document.getElementById('display-frame'),
link=document.getElementsByTagName('a')[0];
//负载处理器
函数onIframeLoad(){
警惕(“感谢您的访问!”);
}
//事件处理程序
if(iframe.addEventListener)iframe.addEventListener('load',onFrameLoad,false);
else if(iframe.attachEvent)iframe.attachEvent('onload','onframeload');
link.onclick=函数(){
iframe.src=this.href;
返回false;
}
};
它可以工作:)

在IE8、ff、铬合金上测试

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(){} );

什么意思?警报消息应该只在单击链接时出现,而不是在初始页面加载时出现。将IE9设置为在IE8模式下工作,您的示例将按照您的需要工作。这是与实际问题代码相反的“示例”代码吗?(我知道ie8模式下的ie9不是一个精确的测试)@Adrian:不,这是我在真实ie8中尝试过的真实代码,而不是在模拟器中。是的,我刚刚试过,如果在真实ie8中“失败”,我可以使用onreadystatechanged事件让它工作……我不认为为“单击”添加标志是一个可靠的解决方案。您最好尝试用javascript编写不引人注目的代码(看看我编辑的答案)。使用IE的
attachEvent
和其他浏览器的
addEventListener
可以解决您的问题。谢谢您的回答!我用我刚想出的解决方案更新了这个问题。请你检查一下我的代码,让我知道你的想法好吗?