Window close not working in ie

Sometimes, Internet Developers are surprised to find that the window.close() API doesn’t always cshed the browser home window.

When looking at the Developer Tools consingle, they’ll check out a message like:

Scripts may cshed only the home windows that were opened up by them.

You watching: Window close not working in ie

Why Do Browsers Limit close()?

Before we dive into what determinants govern what happens once close() is called, it’s important to understand why there’s a restriction at all.

While occasionally described amethod as nebulous “Security reasons!“, the core factor is more aligned via simple user-experience— users could shed necessary state in their internet browser home window or “back/forward” stack (in IE, we dubbed this the TravelLog) if a tab or window went amethod suddenly. The user might’ve been making use of a tab to check out a collection of search results, and if among those outcomes might blow ameans both itself and the backstack with the search results web page, this would certainly be pretty annoying.

There’s a minor anti-abuse/protection argument as well– if a browser tab can blow itself ameans openly, this could be beneficial as a part of scareware or other user-suffer abuses.

What Does the Standard Say?

Here’s what the dom-window-cshed section of the HTML Standard hregarding say:

A looking context is script-closable if it is an auxiliary searching context that was created by a script (as opposed to by an action of the user), or if it is a top-level searching conmessage whose session background consists of just one Document.

This appears simple sufficient, although the components I’ve bolded hide the majority of complexity and also nuance. (Most obviously, “what need to we do if the manuscript was run in response to an action of the user?“)

What Do Browsers Do?

Unfortunately for us, each internet browser has actually a different collection of habits (check out via this test page), partially because a lot of were imposed prior to the typical was composed.

Net Explorer

In Net Explorer, a tab/home window will certainly be closed silently if it was created as a result of a JavaScript home window.open() speak to. There’s no attempt to examine whether the back/forward stack contains just one document: a tab through a huge TravelLog will certainly still close silently if it was opened by script. (IE also permits HTA papers to close themselves without restriction.)

In all other circumstances, the tab/window will not silently close: rather, the user is presented via a one of two modal dialogs, depending upon whether the page represents the only tab in the internet browser window:


*

Chromium (Microsoft Edge / Google Chrome / etc)

As of Chromium 88, window.close() succeeds if the brand-new window/tab has an opener or if the back/forward stack consists of fewer than two entries.

As you can view, tright here are subtle differences right here between what the spec requires and what the web browser implements.

First, notification that I shelp “has an opener” rather than “was developed by a script.” Recontact that the opener property allows a popup home window to refer to the tab that created it.

If the user creates a new tab by clicking the frame button, hitting Ctrl+T, Shift+Clicking a link, or launching a URL from the Shell, the resulting brand-new tab doesn’t have actually an opener set. Any hyperconnect might include rel=opener or rel=noopener to specify whether the brand-new tab has actually an opener set.

As you have the right to see from the over list, both normal link clicks and also JavaScript open() invocations can bring about tabs with or without the opener set. This can be incredibly confusing: Shift+click a connect could lead to a tab that cannot self-cshed, while Left-click that same connect outcomes in a tab that have the right to constantly cshed itself.

See more: Server Error In Application Access Is Denied, Server Error In / Application

Secondly, alert I shelp “entries” quite than “Documents.” In many situations, these are tantamount, however they’re not specifically the same. Consider the case wright here the brand-new tab navigates to a HTML document via a Table of Components at the height. The user clicks on a ToC connect to #Section3 and the browser dutitotally scrolls down to that section. The back/forward stack now has two entries, both pointing to the very same document. Chromium blocks window.close(), but it shouldn’t. This longstanding shortcoming was made more visible in Chromium 88, which now provides links targeting _blank the noopener habits by default.crbug.com/1170131 tracks solving this concern by counting the variety of Documents in the back/forward stack, however it will be tricky to solve bereason presently the renderer process running JavaScript has actually accessibility just to the count of entries in the back/forward stack, not their URLs.

Chromium: User-Experience

When Chrome blocks close(), it emits an alert to the console:

Scripts might cshed only the home windows that were opened by them.

…yet there’s no notification to the user, that may be left perplexed if they clicked on a “Close” switch or connect inside the page. Recently filed crbug.com/1170034 suggests introducing a dialog box favor Web Explorer’s. (As an aside, it additionally sets a new standard for bug filing by consisting of comic-book style art showing unhappy individuals converting to happy customers if the proposed function is landed. :)

Chromium: Exotic Bug Trivia

This is a super-obscure corner case. However, I’ve seen it individually reported versus both Chrome and Edge in the expectations of five years, and it renders me laugh.

If you set Chromium’s On Startup option to “Continue where you left off”, navigate to a page that tries to close itself, and also then close the home window, the web browser will ultimately kill itself on eextremely startup.

It’s hard to gain right into this state, however it’s still feasible in Chrome/Edge 90.

Repro steps: Visit https://webdbg.com/test/opener/. Click the ‘Page that tries to close itself‘ link. Hit Ctrl+Shift+Delete and Clear Browsing History (to clear the back/forward stack). Cshed the web browser through the ‘X’ symbol. Now, attempt to launch the web browser from your start food selection. Giggle wildly as your internet browser launches and then blows itself ameans.

Safari/WebKit

WebKit’s code is comparable to Chromium’s (unsurprising, considering their shared lineage) other than it does not equate navigations prompted through noopener via those created by browser UI. Thus, in Safari, the user might click between many kind of various same-beginning peras and also close() is still allowed.

If close() is blocked, Safari’s (well-hidden) JavaScript console shows:

Can"t close the window since it was not opened by JavaScript

Firefox

Unprefer Chromium, Firefox properly implements the “just one Document” component of the spec. Firefox calls IsOnlyTopLevelDocumentInSHistory which calls IsEmptyOrHasEntriesForSingleTopLevelPage() which enumeprices the background list to check. If tbelow is more than one entry, whether every one of the entries are for the same Document. If they are all for the same record, the close() succeeds.

Firefox provides an about:config override setting called dom.allow_scripts_to_close_windows that bypasses the default restrictions.

When Firefox blocks close(), it emits an alert to the console:

Scripts may not cshed windows that were not opened up by manuscript.

See more: 100 Percent Disk Usage Windows 10 2016, Windows 10 Disk Usage 100% Due To Outlook 2016

There’s an 18 year-old function research for Firefox to display a confirmation dialog rather of failing silently.