Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Call function after window.open

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);
like image 265
pm13 Avatar asked May 29 '12 10:05

pm13


People also ask

How do you call a function after page load?

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.

How do you call a function on window onload?

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.

What is window onload in 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.).

How do I run a script on page load?

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.


1 Answers

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.

like image 137
Denys Séguret Avatar answered Nov 24 '22 01:11

Denys Séguret