Close browser window after opening Custom Protocol

Phill Duffy

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

MaxPRafferty

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Close modal dialog when opening link in new browser tab/window

From Dev

Close modal dialog when opening link in new browser tab/window

From Dev

close browser window after n seconds rails

From Dev

Opening Link using batch file in browser window of custom size

From Dev

Close toggle after opening

From Dev

How make webdriver not to close browser window after each test?

From Dev

Close browser after calling it

From Dev

Close browser after calling it

From Dev

How to close browser window in AngularJS

From Dev

PyQt window closes after opening

From Dev

Tweak TinyMCE Window after opening

From Dev

Error opening a new browser window using javascript

From Dev

Strictly close Browser window without YES/NO option window.close();

From Dev

Close in app browser after a redirect

From Dev

after opening a file check if the file was modified on close

From Dev

Close WPF window after animation

From Dev

Close Popup Window after insert

From Dev

Close Window After Submission of Form

From Dev

close (Chrome) browser window on navbar click

From Java

How to close current tab in a browser window?

From Dev

How to close the browser tab(parent window) with JavaScript

From Dev

Blazor Emit Javascript to Close Browser Window

From Dev

Close Window after open another window?

From Dev

automatically close pylint window after python code window is close in VIM

From Dev

automatically close pylint window after python code window is close in VIM

From Dev

CakePHP not redirecting after opening new window with JavaScript

From Dev

Use JS to open window in place of current browser window and then close it?

From Dev

Flask.session persisting after close browser

From Dev

Auto Login even after browser close in yii

Related Related

  1. 1

    Close modal dialog when opening link in new browser tab/window

  2. 2

    Close modal dialog when opening link in new browser tab/window

  3. 3

    close browser window after n seconds rails

  4. 4

    Opening Link using batch file in browser window of custom size

  5. 5

    Close toggle after opening

  6. 6

    How make webdriver not to close browser window after each test?

  7. 7

    Close browser after calling it

  8. 8

    Close browser after calling it

  9. 9

    How to close browser window in AngularJS

  10. 10

    PyQt window closes after opening

  11. 11

    Tweak TinyMCE Window after opening

  12. 12

    Error opening a new browser window using javascript

  13. 13

    Strictly close Browser window without YES/NO option window.close();

  14. 14

    Close in app browser after a redirect

  15. 15

    after opening a file check if the file was modified on close

  16. 16

    Close WPF window after animation

  17. 17

    Close Popup Window after insert

  18. 18

    Close Window After Submission of Form

  19. 19

    close (Chrome) browser window on navbar click

  20. 20

    How to close current tab in a browser window?

  21. 21

    How to close the browser tab(parent window) with JavaScript

  22. 22

    Blazor Emit Javascript to Close Browser Window

  23. 23

    Close Window after open another window?

  24. 24

    automatically close pylint window after python code window is close in VIM

  25. 25

    automatically close pylint window after python code window is close in VIM

  26. 26

    CakePHP not redirecting after opening new window with JavaScript

  27. 27

    Use JS to open window in place of current browser window and then close it?

  28. 28

    Flask.session persisting after close browser

  29. 29

    Auto Login even after browser close in yii

HotTag

Archive