windows .open后的Javascript调用函数

时间:2022-10-22 15:12:26

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.

然而,使用onload函数被调用得太快了。点击的URL打开一个excel电子表格,从2秒到1分钟下载。

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.

onload函数被称为窗口。已调用open函数。但是,我需要知道什么时候打开了excel文档,而不是什么时候点击了URL。

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);

1 个解决方案

#1


4  

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 :

首先要注意的是,为了避免由于跨域限制而被阻塞(或者不需要在服务器上参数化CORS报头),您必须:

  • serve both your main page and the popup content (your excel file) from the same domain, and the same port
  • 服务于您的主页和弹出的内容(您的excel文件)来自相同的域,和相同的端口。
  • open your main page in http:// and not in file://
  • 以http://打开主页,而不是文件:/

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" :

如果这些条件得到了尊重,最好的解决方案是使用jquery作为其负载函数等待“直到所有的资产,如图像被完全接收”:

<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.

请注意全局方案:当您认为每个浏览器/配置“打开”文件时,它们可能会做一些不同的事情。如果没有合适的插件,没有简单的打开就无法检测,只是下载了它。

#1


4  

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 :

首先要注意的是,为了避免由于跨域限制而被阻塞(或者不需要在服务器上参数化CORS报头),您必须:

  • serve both your main page and the popup content (your excel file) from the same domain, and the same port
  • 服务于您的主页和弹出的内容(您的excel文件)来自相同的域,和相同的端口。
  • open your main page in http:// and not in file://
  • 以http://打开主页,而不是文件:/

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" :

如果这些条件得到了尊重,最好的解决方案是使用jquery作为其负载函数等待“直到所有的资产,如图像被完全接收”:

<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.

请注意全局方案:当您认为每个浏览器/配置“打开”文件时,它们可能会做一些不同的事情。如果没有合适的插件,没有简单的打开就无法检测,只是下载了它。