简体   繁体   English

移动浏览器中的 window.close() 不起作用

[英]window.close() in Mobile Browser not working

Is there an alternative way to close current tab using JS ?有没有其他方法可以使用 JS 关闭当前选项卡? because window.close() is not working on Mobile browsers因为 window.close() 不适用于移动浏览器

I have the same issue, i tried a lot of ways, but none can adapt to all devices.我有同样的问题,我尝试了很多方法,但没有一种方法可以适应所有设备。 At last, I use the follow code, I really have no better way.最后,我使用了以下代码,我真的没有更好的方法。

window.opener=null;
window.open('','_self');
window.close();
window.history.go(-1);
$(document.body).hide()

Some browsers will not allow you to close the window using window.close() unless the script opened the window.除非脚本打开窗口,否则某些浏览器不允许您使用window.close()关闭窗口。 This is a little annoying sometimes.这有时有点烦人。 But there is a workaround to resolve this issue.但是有一种解决方法可以解决此问题。

If you observe the error message that is thrown by Mozilla Firefox, Scripts may not close windows that were not opened by the script .如果您观察到 Mozilla Firefox 抛出的错误消息, Scripts may not close windows that were not opened by the script it clearly says that if the script didn't open the window, you can't close that.它清楚地表明,如果脚本没有打开窗口,则无法关闭它。 But we open a blank page in the same window using “_self” as the target window and close the same window.但是我们使用“_self”作为目标窗口在同一窗口中打开一个空白页面并关闭同一窗口。 In that way, the script opens the window (which is a blank one) and closes the window too.通过这种方式,脚本打开窗口(这是一个空白窗口)并关闭窗口。

So, to put this in the code:所以,把它放在代码中:

<script>
    function closeMe() {
        var win = window.open("","_self"); /* url = "" or "about:blank"; target="_self" */
        win.close();
    }
</script>

and

<input type="button" name="CloseMe" value="Close Me" onclick="closeMe()" />

Ref: link参考: 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM