Forum Moderators: open

Message Too Old, No Replies

window.close problem

         

ksvijay

9:26 am on Jul 28, 2008 (gmt 0)

10+ Year Member



i just created one html page .When i try to close that window ,i am asking the user using confirm box, if they click Cancel button i want to keep the window remains open.

can anyone give the solution, here is the code

<HTML>
<HEAD>
<TITLE>Title</TITLE>

<SCRIPT LANGUAGE="JavaScript">
<!--

function confirmWindow(){
var msg = "Are you want to close this window";

if((confirm(msg)){

}else{
// i want to keep the window remains open

}

}

function close(){
return false;
}
//-->
</SCRIPT>

</HEAD>
<body onunload="confirmWindow()">
</body>
</HTML>

webfoo

2:04 pm on Jul 30, 2008 (gmt 0)

10+ Year Member



What is the purpose of function Close()? It is never called.

Maybe try this:


function confirmWindow(){
var msg = "Do you want to close this window?";

if((confirm(msg)){
// Close window
window.close;
}
else{
// keep window open
return false;
}
}

ksvijay

7:06 am on Jul 31, 2008 (gmt 0)

10+ Year Member



Thanks for the reply, the close method is not used at present, after clicking the close(X) button on the upper right corner , i want to display the confirmation to user "whether you want to close it" and if they click ok button it will close, else if they click cancel button the window remains open, but the return false is not working (IE)

gergoe

12:34 pm on Jul 31, 2008 (gmt 0)

10+ Year Member



You can not monitor when a window is being closed, the only thing you can do is to monitor when a page is being unloaded, and that can be either because the user navigates away from that page (by any means), or the window is being closed. But there's no way I know that you can decide which one happens, and most important, there's no way to cancel the default behavior, that's you can not stop the window being closed.

What you should check out is the

window.onbeforeunload
method, any text returned from that function will result in a confirmation window (handled by the browser), and there the user can choose to stay (cancel the action), or go away (close window or navigate away from the page).

[edit]Although the

window.onbeforeunload
is an elegant solution to your problem, but it is not very well supported - if I remember well.[/edit]

ksvijay

9:41 am on Aug 1, 2008 (gmt 0)

10+ Year Member



Thanks to your reply, shall i override the confirmation message of onbeforeunload ?

gergoe

10:19 am on Aug 1, 2008 (gmt 0)

10+ Year Member



Just try the following:

window.onbeforeunload = function() { 
return "Do you want to leave this page?";
}