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()
}
}
新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。
更多推荐



所有评论(0)