locked
Close the browser RRS feed

  • Question

  • User364663285 posted

    Dear,

    What is the way to close the browser, by one page click event? Why such codes

                    <asp:LinkButton ID="lb_exit" 
                       Text = "Exit"
                       CssClass="lb_exit"
                       Font-Names="Times New Roman" 
                       OnClientClick="var objWindow = window.open(location.href, '_self'); objWindow.close();" 
                       runat="server"/>
    

    can't close the browser?

    Tuesday, October 3, 2017 1:10 PM

Answers

  • User-707554951 posted

    Hi wmec,

    Scripts are not allowed to close a window that a user opened.

    This is considered a security risk.

    Though it isn't in any standard, all browser vendors follow this (Mozilla docs).

    Starting in Firefox 46.0.1, Window.close() can no longer close windows that weren't opened by the same script. This is a security precaution.

    https://developer.mozilla.org/en-US/docs/Web/API/Window/close\

    Best regards 

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 16, 2017 6:53 AM

All replies

  • User753101303 posted

    Hi,

    AFAIK this is a IE only trick. You are supposed to be able to close a window if it was opened programmatically or if you have nothing else in the history.

    Tuesday, October 3, 2017 1:24 PM
  • User364663285 posted

    Hi,

    AFAIK this is a IE only trick. You are supposed to be able to close a window if it was opened programmatically or if you have nothing else in the history.

    How to ensure the window is opened programmablly?

    Tuesday, October 3, 2017 2:48 PM
  • User753101303 posted

    This is done by using window.open but the spec doesn't tell what should happen when the target is the current window. IE overrides how the window was first opened while for other browsers this information is kept and so IE can close any window by using this trick while other browsers are denying this...

    Tuesday, October 3, 2017 4:56 PM
  • User364663285 posted

    Can I say "definitely no way" to close the page, within Chrome, Firefox?

    Wednesday, October 4, 2017 5:09 AM
  • User-707554951 posted

    Hi  wmec,

    Unless you have a handle on every window that was opened (which you can only get if you opened them with javascript) then you cannot force the entire browser to close.

    To close a  current tab you opened, you need to use client side JavaScript code. You can add it to OnClick attribute of Button control, like this:

        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    

    With codebehind:

      protected void Button1_Click(object sender, EventArgs e)
            {
                Button1.Attributes.Add("OnClick", "window.close();");
            }

    Other helpful links for reference:

    https://stackoverflow.com/questions/16521771/close-page-on-click-button-press-asp-net-c-sharp

    http://www.c-sharpcorner.com/forums/close-current-browser-tab-in-asp-net-without-using-javascrip

    https://stackoverflow.com/questions/4391647/chrome-extension-to-loop-to-close-non-selected-tabs

    https://stackoverflow.com/a/22160099

    Best regards

    Cathy

    Wednesday, October 4, 2017 7:45 AM
  • User364663285 posted

    Dear Cathy,

    Does it mean relevant window should have been opened by javascript?

    Thursday, October 5, 2017 2:58 PM
  • User753101303 posted

    Keep in mind that the web is also about keeping the user safe and in control (ie allowing any site to close any window would be abused for creative hacking or even just because you can annoy people).

    The spec that defines this behavior is around http://www.w3.org/TR/html5/browsers.html#script-closable

    A common approach is to show a message before doing this IE hack to have a fallback for browsers that won't close.

    Edit: note that it works if this is the only document in your browsing history so it can appear to work when testing on a single test page (I know this because I helped someone to find why window.close was working when testing but not on his real site).

    Thursday, October 5, 2017 3:45 PM
  • User364663285 posted

    Is it concluded that NO WAY to close window/browser, in Chrome/Firefox?

    Tuesday, October 10, 2017 4:54 AM
  • User-707554951 posted

    Hi wmec,

    Scripts are not allowed to close a window that a user opened.

    This is considered a security risk.

    Though it isn't in any standard, all browser vendors follow this (Mozilla docs).

    Starting in Firefox 46.0.1, Window.close() can no longer close windows that weren't opened by the same script. This is a security precaution.

    https://developer.mozilla.org/en-US/docs/Web/API/Window/close\

    Best regards 

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 16, 2017 6:53 AM