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);
If you wanna call a js function in your html page use onload event. The onload event occurs when the user agent finishes loading a window or all frames within a FRAMESET. This attribute may be used with BODY and FRAMESET elements.
The first approach for calling a function on the page load is the use an onload event inside the HTML <body> tag. As you know, the HTML body contains the entire content of the web page, and when all HTML body loads on the web browser, it will call the function from the JavaScript.
The onload event occurs when an object has been loaded. onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).
You have to call the function you want to be called on load (i.e., load of the document/page). For example, the function you want to load when document or page load is called "yourFunction". This can be done by calling the function on load event of the document.
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 :
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With