简体   繁体   中英

Close currently opened tab

I've tried window.close(); to close the currently opened tab but it does not seem to work. What am I doing wrong? My scope is to run a JavaScript script that automatically closes a manually opened tab.

Thanks.

Typically in javascript (or other flavors of javascript), this is done with...

<a href="#" class="close">Close Window</a>

$(".close").click(function {
    window.close();
});

Note that this will only work on the tab that contains the link that was clicked. It will not close other tabs open in your browser.

I'm giving a +1 for each comment on the question, too.

You can have a look at this answer how-to-close-current-tab-in-a-browser-window

Have a look also at the comments of this answer, where Ryan Joy says:

"This method is only allowed to be called for windows that were opened by a script using the window.open method." In other words, you can only use JavaScript to close a window/tab that was spawned via JavaScript"

Also notice that:

window.close();

works only for Internet Explorer where a popup message appears asking if user agrees for current tab/window to be closed.

In Firefox, Chrome only works for empty tabs and tabs/windows opened via "window.open()" method as mentioned above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM