CodeSky 代码之空

随手记录自己的学习过程

还记得历史长河中的 window.close 么

2019-10-02 20:08分类: JavaScript评论: 0

这次有一个需要关闭页面的需求,于是自然而然想到了经久不衰的 window.open 兄弟款:window.close。在 MDN 中,对于 window.close 的描述也相当简单:window.close

然而我们需要注意的是下面这句话:

Firefox 35.0.1:脚本不能关闭不是他打开的窗口

而切到英文版,你会发现更多详情:

This is no longer the case; for security reasons, scripts are no longer allowed to close windows they didn't open. (Firefox 46.0.1: scripts can not close windows, they had not opened)

当然我也想过,使用各种方法 workaround,结果一查发现,其实在历史中,不同时代不同浏览器会有不同的兼容问题,最佳的兼容性上依旧是将 open 与 close 连用:

w = window.open()
w.close()

这样会关闭新开的那个页面,而如果直接执行 window.close(),可能会报安全错误。

评论 (0)