locked
Window will not close in Edge RRS feed

  • Question

  • User1016820894 posted

    I am having issues closing a window in Edge. In my asp.net code behind I have tried all 3 of the below. But nothing works. It does not close out the window. It works fine in IE. What do I need to do so it will work both in IE and Edge (and Goggle chrome as well). Thanks.

    Response.Write("<script>window.close();</script>");

    Response.Write("<script>window.open('', '_self', ''); window.close();</script>");

    ScriptManager.RegisterStartupScript(Page, typeof(Page), "closePage", "window.close();", true);

    Tuesday, May 12, 2020 12:59 PM

All replies

  • User475983607 posted

    According to the docs, windows.close() might not close the windows that were not opened by the script.   Response.Write and ScriptManager.RegisterStartupScriptthe fall into this category.

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

    You'll need to look into a different approach. 

    Tuesday, May 12, 2020 1:27 PM
  • User1016820894 posted

    Why would it work in IE then?

    Tuesday, May 12, 2020 1:32 PM
  • User475983607 posted

    baldwinjohn

    Why would it work in IE then?

    Again, the docs clearly state "might not work".  The reason is browser behave differently which has been a programming challenge for web developers for many many years.  Rather than asking why, which will not change anything, look into a new approach. 

    https://jeffclayton.wordpress.com/2015/02/13/javascript-window-close-method-that-still-works/

    Tuesday, May 12, 2020 1:46 PM
  • User753101303 posted

    Hi,

    It is a known area where browsers had distinct behaviors and/or bugs allowing to workaround the expected behavior (which may have been fixed since then). I'm not sure there was a real standard spec back then.
    IMO you'll likely have to keep anyway a message asking the user to close the window and/or the browser to have a fallback. This is for a "log off" button?

    I would start by looking closely at the specification to see if it is supposed to work in my situation (note that it having or not an history does make a difference, I saw once here someone who tried and it was fine but it didn't work anymore once going on the relevant page on his real site and it turned out to be that subttle difference).

    Tuesday, May 12, 2020 3:02 PM
  • User-474980206 posted

    Why would it work in IE then?

    because IE is buggy and insecure (and not recommended by MS). As Edge is now a chromium browser like chrome, you need to follow the security rules. see:

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

    this means the window must be opened by script. (window.open). the same origin policy is also in effect.

    note: in the past before the browsers were patched, there were work arounds to fake the script open, but they no longer work.

    Tuesday, May 12, 2020 9:13 PM
  • User288213138 posted

    Hi baldwinjohn,

    I am having issues closing a window in Edge. In my asp.net code behind I have tried all 3 of the below. But nothing works. It does not close out the window. It works fine in IE. What do I need to do so it will work both in IE and Edge (and Goggle chrome as well).

    I tested your code, it works fine in ie and edge, but not work in chrome.

    Chrome cannot close the current window due to security precaution.

    Best regards,

    Sam

    Wednesday, May 13, 2020 6:21 AM
  • User-474980206 posted

    Nor in edge chromium, the current version of  Edge.

    Friday, May 15, 2020 1:53 AM
  • User-10946581 posted

    Hello....Any luck with this issue? 

    We are switching to edge and I have noticed this frustrating issue.

    Thursday, March 18, 2021 12:32 PM
  • User-10946581 posted

    Hi, any luck with this issue?

    Thursday, March 18, 2021 7:24 PM
  • User753101303 posted

    Hi,

    It was an IE bug allowing to mark the current window as being opened by a script rather than by the user. The expected behavior is that a window can be closed by a script if opened by a script (or must have no history which can be misleading if doing a simple test).

    You have many restictions in browser not having an obvious purpose but you have to prevent feature abuse such as maybe here using this for a https://en.wikipedia.org/wiki/Scareware etc...

    Thursday, March 18, 2021 8:40 PM