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()
}
}
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)