Detect if the iframe content has loaded successfully

前端 未结 6 940
天命终不由人
天命终不由人 2020-11-27 19:11

I have a widget that contains an iframe. The user can configure the url of this iframe, but if the url could not be loaded (it does not exists or the user does not have acce

6条回答
  •  失恋的感觉
    2020-11-27 19:33

    If you control the content of the iframe, the iframe can send a message to the parent.

            parent.postMessage('iframeIsDone', '*');
    

    The parent callback listens for the message.

            var attachFuncEvent = "message";
            var attachFunc = window.addEventListener ;
            if (! window.addEventListener) {
                attachFunc = window.attachEvent;
                attachFuncEvent = "onmessage";
            }
    
            attachFunc(attachFuncEvent, function(event) {
                if (event.data ==  'iframeIsDone') { // iframe is done callback here
                }
            });
    

提交回复
热议问题