最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why doesn't IE8 handle iframe onload events? - Stack Overflow

programmeradmin3浏览0评论

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="/" 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="/" target="iframe_a" onclick="activate();">Go!</a>

    </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./" 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./" target="iframe_a" onclick="activate();">Go!</a>

    </body>

</html>
Share Improve this question edited Oct 16, 2012 at 20:40 Mori asked Oct 16, 2012 at 8:31 MoriMori 6,82019 gold badges68 silver badges103 bronze badges 5
  • What do you mean? The alert message should only appear when you click on the link, not on the initial page load. – Mori Commented Oct 16, 2012 at 8:34
  • I dropped your code straight into JSFiddle disabled the libraries. Set IE9 to work in IE8 mode, and your sample works as you want. Is this 'sample' code as oppose to real problem code?( I know ie9 in ie8 mode is not a exact test) – Mesh Commented Oct 16, 2012 at 8:48
  • @Adrian: No, this is the real code that I tried in real IE8, not in an emulator. – Mori Commented Oct 16, 2012 at 8:49
  • Yep just tried it, if does 'fail' in a real ie8, I can get it to work using onreadystatechanged event... – Mesh Commented Oct 16, 2012 at 9:38
  • I don't think adding flag for "click" is a reliable solution. You'll better try unobtrusive way of coding in javascript (look at my edited answer). Using attachEvent for IE and addEventListener for other browsers solve your problem. – Inferpse Commented Oct 17, 2012 at 9:01
Add a ment  | 

4 Answers 4

Reset to default 4

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./" 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./">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>

It seems you can't add a load listener to an iFrame in IE using the DOM property once the page has loaded.

But you can use attachEvent, so:

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

I was testing in IE 6 and reversed the usual test order so that attachEvent is used in preference to addEventListener. You may want to test more recent versions of IE to see if the opposite order works and also test other IE–like browsers such as Opera.

Edit

Modified the code after testing (silly me) to use addEventListener. Here's something that works in IE and others:

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

And if you use an onload attribute in the markup, you don't need to add the listener using script.

It works :)

tested on 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");
    }

Just use jquery:

$(iframe).bind( 'load', function(){} );
发布评论

评论列表(0)

  1. 暂无评论