Making script wait until iframe has loaded before running

后端 未结 3 1723
一个人的身影
一个人的身影 2020-12-16 11:27

The site I\'m working on has a Live Chat plugin on an iframe. I\'m trying to change an image if there are no agents available. My code works on the console, but nothing on t

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 12:23

    Found this from Elijah Manor's website which works very well

    function iFrameLoaded(id, src) {
        var deferred = $.Deferred(),
            iframe = $("").attr({
                "id": id,
                "src": src
            });
    
        iframe.load(deferred.resolve);
        iframe.appendTo("body");
    
        deferred.done(function() {
            console.log("iframe loaded: " + id);
        });
    
        return deferred.promise();
    }
    
    $.when(iFrameLoaded("jQuery", "http://jquery.com"), iFrameLoaded("appendTo", "http://appendto.com")).then(function() {
        console.log("Both iframes loaded");
    });

提交回复
热议问题