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?
You can do it in another way:
In the main document:
In the iframe document (this is, inside the html of the page referenced by url)
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:
It's safer to use
window.parent
since the variablesparent
andtop
could be overwritten (usually not intended).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:I have done this in all kinds of browsers.
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.
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.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.