How to close all browser tabs using JavaScript
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajkiran SwainRank #165 · 14.4K reputationPosted Mar 31, 2023, 3:40 AM EDT
// Get all the windows var windows = window.top.frames; // Loop through each window and close it for (var i = 0; i < windows.length; i++) { windows[i].window.close(); }Jignesh KumarRank #91 · 22.9K reputationPosted Mar 31, 2023, 5:24 AM EDT
Hello Hiren,
You can try this one,
var windows = window.open("", "_self"); windows.close();Deepak RawatRank #646 · 3.7K reputationPosted May 3, 2023, 8:05 AM EDT
This approach is only allowed for windows that were opened by a script, so it won't work for tabs that were opened manually by the user. Here's an example code snippet:
// Get all open windows/tabs var windows = window.top.windows; // Iterate over each window and close it for (var i = 0; i < windows.length; i++) { windows[i].close(); }this code will only work in certain situations, such as when all tabs were opened by the same script. It may not be able to close all tabs in all scenarios, especially if they were opened manually by the user. Additionally, some browsers may block the
window.close()method to prevent abuse by malicious websites.