13

我试图在我的 window.open 函数完全加载后调用一个函数。

但是,使用 onload 函数被调用得太早了。被点击的 URL 会打开一个 Excel 电子表格,下载可能需要 2 秒到 1 分钟。

一旦调用了 window.open 函数,就会调用 onload 函数。但是,我需要知道 Excel 文档何时打开——而不是 URL 何时被打开。

我试过设置一个间隔,但没有被调用:

w = window.open(url,'_parent',false);   

w.onload = function(){
    console.log('here');
    setInterval(function(){
        alert('Hi');
    },10);
4

1 回答 1

5

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.

于 2012-05-29T11:18:21.530 回答