'load' event not firing when iframe is loa

2019-01-07 19:45发布

I am trying to display a 'mask' on my client while a file is dynamically generated server side. Seems like the recommend work around for this (since its not ajax) is to use an iframe and listen from the onload or done event to determine when the file has actually shipped to the client from the server.

here is my angular code:

    var url = // url to my api
    var e = angular.element("<iframe style='display:none' src=" + url + "></iframe>");
    e.load(function() {
        $scope.$apply(function() {
            $scope.exporting = false;  // this will remove the mask/spinner
        });
    });
    angular.element('body').append(e);

This works great in Firefox but no luck in Chrome. I have also tried to use the onload function:

e.onload = function()  { //unmask here }

But I did not have any luck there either.

Ideas?

5条回答
祖国的老花朵
2楼-- · 2019-01-07 19:49

You can do it in another way:

In the main document:

function iframeLoaded() {
     $scope.$apply(function() {
            $scope.exporting = false;  // this will remove the mask/spinner
        });
}

var url = // url to my api
var e = angular.element("<iframe style='display:none' src=" + url + "></iframe>");
angular.element('body').append(e);

In the iframe document (this is, inside the html of the page referenced by url)

    window.onload = function() {
        parent.iframeLoaded();
    }

This will work if the main page, and the page inside the iframe are in the same domain.

Actually, you can access the parent through:

window.parent
parent
//and, if the parent is the top-level document, and not inside another frame
top          
window.top

It's safer to use window.parent since the variables parent and top could be overwritten (usually not intended).

查看更多
乱世女痞
3楼-- · 2019-01-07 19:50

you have to consider 2 points:

1- first of all, if your url has different domain name, it is not possible to do this except when you have access to the other domain to add the Access-Control-Allow-Origin: * header, to fix this go to this link.

2- but if it has the same domain or you have added Access-Control-Allow-Origin: * to the headers of your domain, you can do what you want like this:

var url = // url to my api
var e = angular.element("<iframe style='display:none' src=" + url + "></iframe>");
angular.element(document.body).append(e);
e[0].contentWindow.onload = function() {
    $scope.$apply(function() {
        $scope.exporting = false;  // this will remove the mask/spinner
    });
};

I have done this in all kinds of browsers.

查看更多
唯我独甜
4楼-- · 2019-01-07 19:51

I've just noticed that Chrome is not always firing the load event for the main page so this could have an effect on iframes too as they are basically treated the same way.

Use Dev Tools or the Performance api to check if the load event is being fired at all.

I just checked http://ee.co.uk/ and if you open the console and enter window.performance.timing you'll find the entries for domComplete, loadEventStart and loadEventEnd are 0 - at least at this current time:)

Looks like there is a problem with Chrome here - I've checked it on 2 PCs using the latest version 31.0.1650.63.

Update: checked ee again and load event fired but not on subsequent reloads so this is intermittent and may possibly be related to loading errors on their site. But the load event should fire whatever.

This problem has occurred on 5 or 6 sites for me now in the last day since I noticed my own site monitoring occasionally failed. Only just pinpointed the cause to this. I need some beauty sleep then I'll investigate further when I'm more awake.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-07 19:52

Unfortunately it is not possible to use an iframe's onload event in Chrome if the content is an attachment. This answer may provide you with an idea of how you can work around it.

查看更多
【Aperson】
6楼-- · 2019-01-07 20:06

I hate this, but I couldn't find any other way than checking whether it is still loading or not except by checking at intervals.

var timer = setInterval(function () {
    iframe = document.getElementById('iframedownload');
    var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
    // Check if loading is complete
    if (iframeDoc.readyState == 'complete' || iframeDoc.readyState == 'interactive') {
        loadingOff();
        clearInterval(timer);
        return;
    }
}, 4000);
查看更多
登录 后发表回答