admin管理员组

文章数量:1323203

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

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

So I tried it in a simple HTML page. But there is a problem, I am using my function in a tag. 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 FILE:

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

    <a href = "; onclick="whereAreYouGoing()">Go</a>
    
</body>
</html>

my_javascript_file.js:

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

}

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

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

So I tried it in a simple HTML page. But there is a problem, I am using my function in a tag. 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 FILE:

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

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

my_javascript_file.js:

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

}
Share Improve this question edited Oct 29, 2021 at 20:10 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 24, 2012 at 5:07 hakkihakki 6,5277 gold badges65 silver badges109 bronze badges 4
  • You can only close a window that was opened with the open function source – Errol Fitzgerald Commented Nov 24, 2012 at 5:11
  • no! i dont think like you. If i use <input type="button" onclick="window.close()" /> this closes the window. So close() can close a page. – hakki Commented Nov 24, 2012 at 5:14
  • 2 @hakiko - In most browsers that will only close windows that were opened via JavaScript code. "So close() can close a page." - There's a big difference between closing the window and navigating to a different page. I'm not even sure what you mean by "close a page"? – nnnnnn Commented Nov 24, 2012 at 5:22
  • you are right @nnnnnn window.close() is unnecessary for this example. If user click to OK buton <a> tag chance the page :) – hakki Commented Nov 24, 2012 at 5:28
Add a ment  | 

2 Answers 2

Reset to default 3

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

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

<a href = "http://www.google." 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.

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

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

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.

本文标签: htmlJavascript windowclose() and windowstop() Logical or TechnicalStack Overflow