Detect browser close event or redirection in javascript

Posted by Surajemo under JavaScript on 11/5/2012 | Points: 10 | Views : 11573 | Status : [Member] | Replies : 4
"I want solution for google chrome only cos my code is working fine in IE "

I am trying to use Javascript in order to detect when a user
presses the little 'X' at the top right hand corner of the window i want to execute this code but this code is not working goggle chrome
window.onbeforeunload = function()
// {
// if ((window.event.clientx < 0) || (window.event.clienty < 0)) {
// // alert("close");
// window.opener.location.href = 'requestmeeting2.aspx';
// }
// if (event.altkey == true && event.keycode == 0) {
// //alert("alt + f4 being pressed");
// window.opener.location.href = 'requestmeeting2.aspx';
// }
// }




Responses

Posted by: Seng2hs on: 11/5/2012 [Member] Starter | Points: 25

Up
0
Down
try this
<script type=\"text/javascript\">
var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
var leave_message = 'You sure you want to leave?'
function goodbye(e)
{
if(dont_confirm_leave!==1)
{
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation)
{
e.stopPropagation();
e.preventDefault();
}

//return works for Chrome and Safari
return leave_message;
}
}
window.onbeforeunload=goodbye;
</script>

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Surajemo on: 11/5/2012 [Member] Starter | Points: 25

Up
0
Down
thanks for the reply
i tried that
it does work but i does not redirects
i tried writing
<script type="text/javascript">
var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
var leave_message = window.opener.location.href = 'Requestmeeting2.aspx';
function goodbye(e)
{
if(dont_confirm_leave!==1)
{
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.

if ((window.event.clientx < 0) || (window.event.clienty < 0))
{
e.cancelBubble = true;
e.returnValue = window.opener.location.href = 'REQUESTMEETING2.aspx';
}
//e.stopPropagation works in Firefox.
if (e.stopPropagation)
{
e.stopPropagation();
e.preventDefault();
}

//return works for Chrome and Safari
if ((window.event.clientx < 0) || (window.event.clienty < 0)) {
// alert("close");
return window.opener.location.href = 'REQUESTMEETING2.aspx';
}
// return leave_message;
}
}
// window.onbeforeunload=goodbye;

</script>
it keeps on refreshing

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Seng2hs on: 11/5/2012 [Member] Starter | Points: 25

Up
0
Down
Use this code, dont change anything

<script type="text/javascript">
var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
var leave_message = "'You sure you want to leave";
function goodbye(e) {
if (dont_confirm_leave !== 1) {
if (!e) e = window.event;

//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
else {
alert("Test");
window.location.href = 'requestmeeting2.aspx';
}

}
}

window.onbeforeunload=goodbye;

</script>

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Surajemo on: 11/5/2012 [Member] Starter | Points: 25

Up
0
Down
no its not working requestmeeting2.aspx' is not get called

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response