Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close browser window after opening Custom Protocol

I have created an ASP.Net application page to handle opening FileSite links. There is a custom protocol which is handling the links correctly, i.e it opens the files, however it leaves me with an empty browser page as the file is launched.

I have 3 scenarios I am working with

  1. Links directly to the handling page will launch the file and close the browser
  2. Links from another page on the Intranet will launch handling page, open the file and return to the originating page
  3. Links from a dialog on the Intranet open the handling page, launch the file and then close the handling page

The code I have is the following (Codebehind is setting the FileUrl and choosing which function to call of the two)

<script type="text/javascript" language="javascript">

    // Files opened directly from link
    function OpenFileSiteLink() {
        window.location.href = '<%= FileUrl %>';
    }

    // Files opened from within Intranet
    function OpenFileSiteLinkReferrer(referrer, dialogOpened) {

        window.open('<%= FileUrl %>');

        if (dialogOpened) {
            window.open('close.html', '_self');
        } else {
            window.location.href = referrer;
        }
    }

</script>

The code in the close.html file has only the following

 <script type="text/javascript"> window.close();</script>

This was taken from How can I close a browser window without receiving the "Do you want to close this window" prompt?

Any suggestions how I can open the protocol to launch the application without the additional dialog would be appreciated

like image 888
Phill Duffy Avatar asked Jul 09 '13 10:07

Phill Duffy


People also ask

How do I close a window not open in a script?

Step 2: Close this open window using the close() method: The window. close() method closes the window on which it is called. The window that was opened in the first step is closed by using this method. This works because the window has now been opened by our script instead of the user.

How do I close a browser window in HTML?

JavaScript provides an in-built function named close() to close the browser window that is opened by using window.

How do I close a window using jquery?

click(function(){ window. close(); }); Note: you can not close any window that you didn't opened with window. open .

What is custom protocol?

A custom protocol can be assigned the same name as a pre-defined protocol, in order to extend the number of IP addresses or ports associated with the original protocol. See Adding to a pre-defined protocol for more information.


2 Answers

The least hacky and most reliable method to do this is the most annoying to implement. Unfortunately, IE 9/10 and Firefox have plugged up the regular methods of accomplishing this, so you may have no other choice.

The strategy is to come at the popup from the parent window, rather than opening it directly. You will need to create a function to load the appropriate url in a popup, and then apply it to the onclick of every link on your linking to the url of the handler. Then, include this script on every page on your site. I am also assuming that each file url is unique, but has a common base url of some kind. If this is not the case, you will also need to set an identifying class or attribute. The function to do the replacement would be something along the lines of:

var replaceHandlerLinks = function () {
    var fileLinks = document.querySelectorAll("[href*='/beginning/to/file/path']");
    for (var i = 0; i < fileLinks.length; i++) {
        fileLinks[i].onclick = function () {
            var fileOpener = window.open(fileLinks[i].href);
            //depending on how long your file takes to load, you may need to wait here
            fileOpener.close();
        }
    }
}

The obvious downside of this being that any link to these files would need to originate from pages you control. Additionally, note that I am using document.querySelectorAll to target the links href element. This means that buttons etc will not work with this particular function. Using a more browser compatible/robust query with JQuery or setting a class on all required buttons,links,etc would make this a more complete approach.

Another similar approach, if you need to link from pages you do not control, would be to always link to the same page from everywhere with a param, eg "/openfile.aspx?file=actualFilePath", then from that page open a popup to load the file, watch the new window and close it when the file is done. Then, attempt to redirect the current window based on scenario: 1) go to homepage/landing/etc 2) go to referrer 3) go to intranet landing/ referrer if possible. This isn't as elegant as just closing a popup from within itself, but it addresses the ugly blank window.

Neither approach is perfect, but outside of restricting users to ie8- and chrome, you may not have any other choice.

like image 181
MaxPRafferty Avatar answered Sep 22 '22 15:09

MaxPRafferty


What about if you try to navigate into a <iframe> instead of a new window?

Try to add a new <iframe> to the DOM, navigate to your custom url, then remove the <iframe> from the DOM. Do not bother to load the close.html page.

(disclaimer: to be honest, it's just an idea, I did no tried)

like image 28
Steve B Avatar answered Sep 24 '22 15:09

Steve B