Scripts may close only the windows that were opened by them.脚本只能关闭它们打开的窗口。

当分享链接访问页面,使用window.close()关闭当面页面会报错。

window.close()

Scripts may close only the windows that were opened by them.

有些浏览器可能会阻止通过脚本来关闭窗口,尤其是在不是通过脚本打开的窗口(如用户手动输入或通过链接打开的窗口)。这是为了避免恶意脚本滥用这个功能。因此,这段代码可能不适用于所有浏览器和情况。

使用这段代码代替window.close(),就能关闭当前页面。

//关闭页面
const closePage = () => {
  if (navigator.userAgent.indexOf('Firefox') != -1 || navigator.userAgent.indexOf('Chrome') != -1) {
    window.location.href = 'about:blank'
    window.close()
  } else {
    window.opener = null
    window.open('', '_self')
    window.close()
  }
}

你可能感兴趣的:(javascript)