本文介绍了window.open 后的 Javascript 调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试完全加载window.open函数之后调用一个函数.

I'm trying to call a function after my window.open function has fully loaded.

但是,使用onload函数的调用太早了.所命中的URL将打开一个excel电子表格,下载可能需要2秒钟到1分钟的时间.

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.

在调用window.open函数之后,就会立即调用onload函数.但是,我需要知道何时打开excel文档-而不是何时单击URL.

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

推荐答案

首先请注意,为了避免跨域限制(或不必在服务器上参数化CORS标头)而阻止此操作,您必须:

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 :

  • 在同一域和同一端口中同时服务您的主页和弹出内容(您的excel文件)
  • http://中打开主页,而不在 file://
  • 中打开主页
  • 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://

如果满足这些条件,最好的解决方案是使用jquery作为其 load 函数等待直到所有资产(例如图像)都已完全收到":

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>

请谨慎使用全局方案:当您认为每个浏览器/配置打开"文件时,它们可能会做不同的事情.没有一个简单的 open 来检测是否决定关闭它,如果没有合适的插件,只需下载它即可.

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.

这篇关于window.open 后的 Javascript 调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 11:25