Cross domain iframe content load detection

前端 未结 6 1124
猫巷女王i
猫巷女王i 2020-12-01 11:22

I have a rather interesting problem. I have a parent page that will create a modal jquery dialog with an iframe contained within the dialog. The iframe will be populated wit

6条回答
  •  伪装坚强ぢ
    2020-12-01 11:33

    Nicholas Zakas has an article about detecting if an iframe loaded: http://www.nczonline.net/blog/2009/09/15/iframes-onload-and-documentdomain/. Basically you have this code snippet:

    var iframe = document.createElement("iframe");
    iframe.src = "simpleinner.htm";
    
    if (iframe.attachEvent){
        iframe.attachEvent("onload", function(){
            alert("Local iframe is now loaded.");
        });
    } else {
        iframe.onload = function(){
            alert("Local iframe is now loaded.");
        };
    }
    
    document.body.appendChild(iframe);
    

    I haven't tested it, but I'm pretty sure jQuery should be able to handle it by doing something like $("#iframe").load(function () { alert("Local iframe is now loaded."); });

提交回复
热议问题