为什么IE8不处理iframe onload事件? [英] Why doesn't IE8 handle iframe onload events?

查看:136
本文介绍了为什么IE8不处理iframe onload事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例代码:

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

它适用于所有主流浏览器,没有问题,但IE8(以及可能的先前版本)不支持明白它。

更新:刚刚提出了一个解决方案,但我不确定它是否正确编码。请仔细阅读:

 <!DOCTYPE html> 
< html>

< head>
< title>< / title>
< script>
var clicked = false;

函数activate(){
clicked = true;


function pop(){
if(clicked){
alert('Thanks for the visit!');
};
}
< / script>
< / head>

< body>
< iframe name =iframe_aonload =pop();>< / iframe>
< a href =http://www.example.com/target =iframe_aonclick =activate();> Go!< / a>

< / body>

< / html>


解决方案

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

 <!DOCTYPE html> 
< html>
< head>
< title>< / title>
< script>
函数onIframeLoad(iframe){
if(iframe.src){
alert('Thanks for the visit!');
}
}
函数onLinkClick(url){
document.getElementById('iframe_a')。src = url;
}
< / script>
< / head>
< body>
< iframe id =iframe_aonload =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'),$ b $ link = document.getElementsByTagName('a')[0];

//载入处理函数
函数onIframeLoad(){
alert('Thanks for the visit!');
}

//事件处理程序
if(iframe.addEventListener)iframe.addEventListener('load',onIframeLoad,false);
else if(iframe.attachEvent)iframe.attachEvent('onload',onIframeLoad);

link.onclick = function(){
iframe.src = this.href;
返回false;
}
};
< / script>
< / body>
< / html>


Sample code:

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

It works in all major browsers with no problem, but IE8 (and probably prior versions) don't understand it.

Update: Just came up with a solution, but I'm not sure if it's right coding. Please review:

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

解决方案

Using inline attribute on iframe seems to fix this issue in 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>

update by request:

You should try writing more unobtrusive javascript. Writing code in such way may prevent you from such strange bugs in 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>

这篇关于为什么IE8不处理iframe onload事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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