NEW 221547
history.length starts at 2 in a new tab opened by target="_blank"
https://bugs.webkit.org/show_bug.cgi?id=221547
Summary history.length starts at 2 in a new tab opened by target="_blank"
potassium634iodide828
Reported 2021-02-08 05:51:43 PST
Created attachment 419581 [details] Demonstration of this bug Safari version: Safari 604.1 iOS version: iOS 14.2 Sample URL: https://ss1.xrea.com/ango.g3.xrea.com/jkkn/safari_bug.html Step to reproduce the issue: 1. Open a new tab via target="_blank" 2. Check history.length Expected results: history.length should start at 1. All of Google Chrome for iPhone (Chrios), Chrome for Android, Firefox for iOS (Fxios), Chrome for Desktop, and Firefox for Desktop return 1 correctly. Even MDN says " For example, for a page loaded in a new tab this property returns 1." https://developer.mozilla.org/en-US/docs/Web/API/History/length Actually results: history.length starts at 2. Only Safari for iOS and iPadOS incorrectly return 2 in a new tab. *Note: This bug might be related to Bug 221546: https://bugs.webkit.org/show_bug.cgi?id=221546
Attachments
Demonstration of this bug (3.33 MB, video/mp4)
2021-02-08 05:51 PST, potassium634iodide828
no flags
Radar WebKit Bug Importer
Comment 1 2021-02-15 05:52:15 PST
Chris Dumez
Comment 2 2021-02-23 08:52:08 PST
I think this may be a side effect of Safari's "back to close tab" feature. In the user interface, you are also able to press the back button and it closes the tab.
potassium634iodide828
Comment 3 2021-02-23 16:55:46 PST
(In reply to Chris Dumez from comment #2) > I think this may be a side effect of Safari's "back to close tab" feature. > In the user interface, you are also able to press the back button and it > closes the tab. Thank you for checking this bug. I love the behavior of the "back to close tab" feature, but other browsers (e.g. Chrome) don't have the feature. To implement the same behavior of the "back to close tab" feature in cross-browser environment, I write a code in my website like this: <button id="close">Close or back</button> <script> const close = document.getElementById("close"); if ( history.length === 1 ) { close.addEventListener("click", () => { window.close(); }, false); } else { close.addEventListener("click", () => { history.back(); }, false); } </script> This code enables the similar behavior to Safari's "back to close tab" feature in other browsers, which don't have the feature. But ironically, this code doesn't work as expected in the very Safari, because of Bug 221546 and Bug 221547 (this one). Because other browsers only allows window.close(); to work when history.length===1, I had to write the code like this. But in Safari, the else clause is executed because of Bug 221547. Furthermore, the button doesn't close the tab correctly because of Bug 221546. To overcome these problems, both Bug 221546 and Bug 221547 should be fixed. Otherwise, I'll need to use browser sniffing to detect Safari, which is an ugly way. The "back to close tab" feature is great, but it shouldn't affect history.length.
potassium634iodide828
Comment 4 2021-02-23 17:18:33 PST
I made a sample: https://ss1.xrea.com/ango.g3.xrea.com/jkkn/safari_bug_sample.html Please feel free to use this sample in debugging these bugs.
Note You need to log in before you can comment on or make changes to this bug.