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.
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.
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.
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).
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:
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
4 Points
66 Posts
Window will not close in Edge
May 12, 2020 12:59 PM|baldwinjohn|LINK
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);
All-Star
53021 Points
23607 Posts
Re: Window will not close in Edge
May 12, 2020 01:27 PM|mgebhard|LINK
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.
Member
4 Points
66 Posts
Re: Window will not close in Edge
May 12, 2020 01:32 PM|baldwinjohn|LINK
Why would it work in IE then?
All-Star
53021 Points
23607 Posts
Re: Window will not close in Edge
May 12, 2020 01:46 PM|mgebhard|LINK
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/
All-Star
48510 Points
18071 Posts
Re: Window will not close in Edge
May 12, 2020 03:02 PM|PatriceSc|LINK
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).
All-Star
58174 Points
15650 Posts
Re: Window will not close in Edge
May 12, 2020 09:13 PM|bruce (sqlwork.com)|LINK
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.
Contributor
3370 Points
1409 Posts
Re: Window will not close in Edge
May 13, 2020 06:21 AM|samwu|LINK
Hi baldwinjohn,
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
All-Star
58174 Points
15650 Posts
Re: Window will not close in Edge
May 15, 2020 01:53 AM|bruce (sqlwork.com)|LINK
Nor in edge chromium, the current version of Edge.