Javascript window.close() 和 window.stop() 逻辑还是技术?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13538452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:05:31  来源:igfitidea点击:

Javascript window.close() and window.stop() Logical or Technical?

javascripthtmljavascript-events

提问by hakiko

maybe my question is very simple but i am new at HTML and JavaScript.

也许我的问题很简单,但我是 HTML 和 JavaScript 的新手。

I want to learn window.close()and window.stop(),

我想学习window.close()window.stop()

so i tried it in a simple HTML page. But there is a problem, i'am using my function in atag. and window.stop()does not stop the operation. I want to stop the redirection if confirm dialog not true. How can i fix this problem?

所以我在一个简单的 HTML 页面中尝试了它。但使用我的函数中存在的问题,我'一个标签。并且window.stop()不会停止操作。如果确认对话框不正确,我想停止重定向。我该如何解决这个问题?

HTML FILE:

HTML文件:

<html>
<head>
    <title> My page </title>    
    <script src="my_javascript_file.js"></script>   
</head>
<body>

    <a href = "http://www.google.com" onclick="whereAreYouGoing()">Go</a>

</body>
</html>

my_javascript_file.js:

my_javascript_file.js:

function whereAreYouGoing(){

    var exit = confirm("Are You Want to Sure to Leave?");

    if(exit){
        window.close();
    }

    else
        window.stop();

}

回答by nnnnnn

That's not what window.stop()is for.

那不是window.stop()为了什么。

Since you're using an inline onclickhandler you can prevent the default anchor navigation by returning false:

由于您使用的是内联onclick处理程序,因此您可以通过返回来阻止默认锚导航false

<a href = "http://www.google.com" onclick="return whereAreYouGoing()">Go</a>

function whereAreYouGoing(){
    var exit = confirm("Are You Want to Sure to Leave?");
    if(exit){
       window.close();
    } else {
       return false;
    }
}

Although window.close()will attempt to close the current window - which in most browsers won't work unless the current window was opened via JavaScript in the first place. And if it does work it obviously won't then navigate to the specified URL.

虽然window.close()会尝试关闭当前窗口 - 在大多数浏览器中这将不起作用,除非首先通过 JavaScript 打开当前窗口。如果它确实有效,它显然不会导航到指定的 URL。

It makes more sense to simply allow the default navigation (to google.com, in your example) if the user clicks OK:

如果用户单击“确定”,则简单地允许默认导航(在您的示例中为 google.com)更有意义:

function whereAreYouGoing(){
    return confirm("Are You Want to Sure to Leave?");
}

回答by Kneel-Before-ZOD

Try putting

尝试放置

 
  window.onbeforeunload = whereAreYouGoing;
 

at the beginning of the javascript code.
onbeforeunload always runs before a window is closed; so, if you wire the function to it, it'll call the function before closing the window.
Although that will display the dialog to the user before closing the window, all browsers gives the user the opportunity to override the function; this means that if the user wants to close the window, the browser will close the window, even if your function tries to prevent that from happening.

Hope this helps.

在 javascript 代码的开头。
onbeforeunload 总是在窗口关闭之前运行;因此,如果您将函数连接到它,它会在关闭窗口之前调用该函数。
尽管这会在关闭窗口之前向用户显示对话框,但所有浏览器都为用户提供了覆盖该功能的机会;这意味着如果用户想要关闭窗口,浏览器将关闭窗口,即使您的函数试图阻止这种情况发生。

希望这可以帮助。