I'm trying to call a function after my window.open function has fully loaded.
However, using the onload function is being called too soon. The URL that's being hit opens an excel spreadsheet and can take from 2 secs to 1 min to download.
The onload function is being called as soon as the window.open function has been called. However, I need to know when the excel doc has been opened - not when the URL was hit.
I've tried setting an interval but that's not being called:
w = window.open(url,'_parent',false);
w.onload = function(){
console.log('here');
setInterval(function(){
alert('Hi');
},10);
First note that in order to do this without being blocked because of cross-domain restrictions (or without having to parameterize CORS headers on your server), you must :
- serve both your main page and the popup content (your excel file) from the same domain, and the same port
- open your main page in
http://
and not in file://
If those conditions are respected, the best solution is to use jquery as its load function waits "until all assets such as images have been completely received" :
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<script>
var popup = window.open('popup.html');
$(popup.document).load(function() {
alert('loaded');
// do other things
});
</script>
</body>
</html>
Be careful with your global scheme : each browser/configuration may do something different when you think they "open" the file. There's no way to detect with a simple open
if they decided to dismiss it, hadn't the proper plugin, simply downloaded it.