How can I close the browser window after redirecting to an Excel file automatically opened in Excel?
In the web application I am working on, we have some long-running processes that are in charge of generating Excel documents. We basically open a popup with window.open
which takes care of polling the server to report the process status and to wait for the availability of the document, and redirect to the generated document when it is ready (window.location = THE_URL_OF_THE_FILE
).
When the re开发者_JS百科direction happens, depending on the user configuration (see Microsoft Knowledge base), the Excel sheet may be opened/embedded directly in Internet Explorer, or it may open the file in a new instance of Excel.
In the latter case, the document opens successfully in Excel, but the window that caused its opening stays there forever.
Is there a way to force that window to close as soon as Excel has successfully loaded the file ?
I expect this is somehow related to how the integration between IE and Windows works ...
One option is to have the popup window do the redirection (via meta-redirect, javascript, etc...), then use a javascript timeout to close the window after allowing a bit of time for the browser to fetch the Excel file.
<html>
<head>
<meta http-equiv="refresh" content="0;http://example.com/yourfile.xls" />
<script type="text/javascript">
function closeMe() {
window.close();
}
setTimeout(closeMe, 5000);
</script>
</head>
<body>
</body>
</html>
If Excel popups inside the browser, then the html/js will vanish. Otherwise, Excel will fire up in a separate window, and then the JS kicks in 5 seconds later to close the popup.
Shooting from the hip here, but I would set a global variable (javascript) in the parent window and then check for it after my polling window has opened the excel document. If the variable still exists, you know that the document has opened in excel. If the variable doesn't exist, you know that the excel document opened in the browser.
精彩评论