需求
最近有一个需求,需要关闭前一个页面打开的新标签页。
查了很多资料,最终解决问题。
关闭标签页所需条件
- 标签页必须由js代码打开
- window.opener不能未null
- 当前标签页history栈只能有一条
具体代码
// 以下两种方式都可以
window.close()
open(location.href, '_self').close()
参考资料
https://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window
https://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome/19768082#19768082