'How can I close the browser window after redirecting to an MS office (Word, Excel, PowerPoint) document automatically opened?

In the web application I am working on, we have some long-running processes that are in charge of getting office 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 redirection happens, depending on the user configuration (see Microsoft Knowledge base), the MS office document may be opened/embedded directly in Internet Explorer, or it may open the file in a new instance of office suit.

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

EDIT

$.ajax({
           async: false,
           type: 'Post',
           dataType: 'json',
           url: '/Controller/Action/' + docId,
           data: $('form').serialize(),
           success: function (url) {
               window.open(url, 'ShowFile', 'top=25,left=50,width=680,height=400,help=1,status=1,scrollbars=1,toolbars=1,menubar=1,resizable=1',false);
       }
   });

The document is opened in a new word instance and then the window remains open after the document is opened, but the window should close when the file is opened in a new Ms office instance. Url is location on the server pointing to the document.



Solution 1:[1]

This is not possible. For many reasons:

  1. After you change the window's location, you cannot execute any code - you're done
  2. The document opened in a new window cannot communicate with the old window, because it is not an HTML page and you cannot execute any JavaScript there
  3. There is no way of telling how the document will be handled. You can do some guess work depending on the browser type and OS being used, but that will always be a guess. It could either be downloaded, opened in some plugin, opened natively in Chrome Frame (upcoming feature), opened automatically in MS Word... the possible outcomes are endless.

I suggest you rethink your website logic.

I would suggest performing some AJAX instead of opening a new window and use that to check the status of the document, and when it's ready, open it in a new window.

If you really need to open a new window to do that, I suggest you change the document.location of your parent window (the one you've used to open the pop up). After that close the pop up. This will not redirect the page to a new location but open the document as you've described (however that behaviour cannot be guaranteed depending on the user's setup).

Solution 2:[2]

Following is the code snippet for window open/close using Javascript (source: http://www.w3schools.com/jsref/met_win_close.asp). You can probably modify it for your needs:

<html>
<head>
<script>
function openWin()
{
myWindow=window.open("","","width=200,height=100");
myWindow.document.write("<p>This is 'myWindow'</p>");
}

function closeWin()
{
myWindow.close();
}
</script>
</head>
<body>

<input type="button" value="Open 'myWindow'" onclick="openWin()">
<input type="button" value="Close 'myWindow'" onclick="closeWin()">

</body>
</html>

You can use jQuery and implement sort of call-back on window.open. Also, if the window is opened from another window by javascript, then there is a parent property; it can be used to communicate between two windows.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2