hakkikonu hakkikonu - 1 year ago 134
HTML Question

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

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?


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

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



function whereAreYouGoing(){

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




Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download