iframe behaviour of onload vs addEventListener('load')

前端 未结 3 1827
南旧
南旧 2021-02-01 06:32

I\'ve been playing around with adding hidden iframe elements to a page, and I want to manipulate the DOM of the these once loaded. I\'ve noticed that I can\'t start manipulating

相关标签:
3条回答
  • 2021-02-01 06:51

    This is working for me:

    html:

    iframe source code: <br />
    <textarea id="output" rows="20" cols="60">loading ...</textarea>
    

    javascript (on documentReady):

    var iframe = document.createElement('iframe');
    iframe.id = iframe.name = "testframe";
    iframe.src = "http://fiddle.jshell.net";
    iframe.width = 400;
    iframe.height = 100;
    iframe.style.display = "none";
    
    if (iframe.addEventListener)
        iframe.addEventListener("load", loaded, false);
    else
        iframe.attachEvent("onload", loaded);
    
    function loaded() {
        var html;
        if (iframe.contentDocument)
            html = iframe.contentDocument.getElementsByTagName("HTML")[0].innerHTML;
        else
            html = window.frames[iframe.name].document.getElementsByTagName("html")[0].innerHTML;
    
        document.getElementById("output").value = html;
    }
    
    document.getElementsByTagName("body")[0].appendChild(iframe);
    

    See the Demo at: http://jsfiddle.net/WcKEz/

    Works with addEventListener, but includes the fallback to attachEvent. Access to the DOM of the IFRAME of course only on the same domain.

    0 讨论(0)
  • 2021-02-01 06:57

    addEventListener() function needs 3 arguments! Take a look at https://developer.mozilla.org/en/DOM/element.addEventListener

    The 3rd argument is marked as optional, but then they write:

    Note that this parameter is not optional in all browser versions.

    I'm not sure when and where it is required, but my tests on FF4 threw an exception when calling the addEventListener with 2 arguments:

    uncaught exception: [Exception... "Not enough arguments" nsresult: "0x80570001 (NS_ERROR_XPC_NOT_ENOUGH_ARGS)" location: "JS frame :: http://localhost/index.php :: :: line 10" data: no]

    By the way, your code works well in Chrome [the string loaded! is logged in console].

    Like FF, IE9 needs the 3rd argument in the standards mode (with <!DOCTYPE html>). IE9 is the first IE that supports W3C's event model. So in the earlier versions we need to try attachEvent. I don't have earlier IEs, but it worked in IE7/8 Standards Mode and even in Quirks Mode in IE9. Here is the code I used:

    <!DOCTYPE html>
    <html>
    <head><title></title></head>
    <body>
    <script>
        window.onload=function(){
            var iframe = document.createElement('iframe');
            var func = function() { console.log('loaded!');};
            if(iframe.addEventListener)
                iframe.addEventListener('load', func, true);
            else if(iframe.attachEvent)
                iframe.attachEvent('onload',func);
            document.body.appendChild(iframe);
        }
    </script>
    </body>
    </html>
    
    0 讨论(0)
  • 2021-02-01 06:58

    Does the first example work? Not sure exactly what you're looking for, but this should illuminate when events work: jQuery document.ready source

    $(document).ready equivalent without jQuery

    addEventListener does not work in IE, so if that's where you're testing, then the 2nd would fail before the iframe gets appended.

    You could also add a callback from the page itself, though (for example, using jQuery so you don't reinvent the wheel) I suspect $(iframe).ready() {..} would give you consistent behavior.

    0 讨论(0)
提交回复
热议问题