![]() ![]() The user clicks on a ToC link to #Section3 and the browser dutifully scrolls down to that section. Consider the case where the new tab navigates to a HTML document with a Table of Contents at the top. Secondly, notice I said “entries” rather than “Documents.” In most cases, these are equivalent, but they’re not exactly the same. This can be very confusing: Shift+click on a link might result in a tab that cannot self-close, while Left-click on that same link results in a tab that can always close itself. A open() JavaScript call can specify noopener in its windowFeatures string to set the new tab’s opener to null.Īs you can see from the above list, both normal link clicks and JavaScript open() invocations can result in tabs with or without the opener set.Any hyperlink may include rel=opener or rel=noopener to specify whether the new tab has an opener set.In contrast, if the tab is opened via open() or hyperlink with a named target ( not _blank), then by default, the opener property is set.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 new tab doesn’t have an opener set.In all other circumstances, the tab/window will not silently close: instead, the user is presented with a one of two modal dialogs, depending on whether the page represents the only tab in the browser window:Ĭhromium (Microsoft Edge / Google Chrome / etc)Īs of Chromium 88, window.close() succeeds if the new window/tab has an opener or if the back/forward stack contains fewer than two entries.Īs you can see, there are subtle differences here between what the spec requires and what the browser implements.įirst, notice that I said “has an opener” rather than “was created by a script.” Recall that the opener property allows a popup window to refer to the tab that created it. (IE also allows HTA documents to close themselves without restriction.) There’s no attempt to check whether the back/forward stack contains only one document: a tab with a large TravelLog will still close silently if it was opened by script. ![]() In Internet Explorer, a tab/window will be closed silently if it was created as a result of a JavaScript window.open() call. Unfortunately for us, each browser has a different set of behaviors ( explore with this test page), partly because most were implemented before the standard was written. (Most obviously, “what should we do if the script was run in response to an action of the user?“) What Do Browsers Do? This seems simple enough, although the parts I’ve bolded hide a lot of complexity and nuance. Here’s what the dom-window-close section of the HTML Standard has to say:Ī browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user ), or if it is a top-level browsing context whose session history contains only one Document. There’s a minor anti-abuse/security argument as well– if a browser tab could blow itself away freely, this might be useful as a part of scareware or other user-experience abuses. The user might’ve been using a tab to explore a set of search results, and if one of those results could blow away both itself and the backstack with the search results page, this would be pretty annoying. While sometimes explained away as nebulous “ Security reasons!“, the core reason is more aligned with simple user-experience- users could lose important state in their browser window or “back/forward” stack (in IE, we called this the TravelLog) if a tab or window went away unexpectedly. Why Do Browsers Limit close()?īefore we dive into what factors govern what happens when close() is called, it’s important to understand why there’s a restriction at all. ![]() Scripts may close only the windows that were opened by them. ![]() When looking at the Developer Tools console, they’ll see a message like: Sometimes, Web Developers are surprised to find that the window.close() API doesn’t always close the browser window. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |