Hi,
Even I am trying the browser close event for cross browser and this should trigger only when X button is clicked or page is refreshed. Here's the code pasted above which I am using but it only works in IE.
I know the issue is because of window.events and ClientX/ClientY.
Could someone please fix my code to make it work in all browsers? This is very urgent and have been striving since 3 days...

*Please Note: Current code is working in IE only and to check you have to close the browser window or refresh the page with mouse click

=====================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><head>
<title>Warning Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript">

// warning message javascript

var isOpera, isIE, isNav, isFox, isOther = false;
if (navigator.userAgent.indexOf("Opera") != -1) {
isOpera = true;
} else if (navigator.userAgent.indexOf("Firefox") != -1) {
isFox = true;
} else if (navigator.appName == "Microsoft Internet Explorer") {
isIE = true;
} else if (navigator.appName == "Netscape") {
isNav = true;
} else {
isOther = true;
}

if (isIE || isOpera || isNav || isFox || isOther) {
window.onbeforeunload = WarnUser;
function WarnUser() {
OffsetX = window.event.clientX;
OffsetY = window.event.clientY;
if (((window.event.clientX < 0) || (window.event.clientY < 0))
&& (isWarnUser == true)) {
event.returnValue = " ";
//window. önunload = sessionInvalidate;
} else {
// Reset the flag to its default value.
isWarnUser = true;
}
}
}




</script>

</head>
<body>
<script type="text/javascript">
var isWarnUser = true;
</script>
test
</body>
</html>
========================

Recommended Answers

All 3 Replies

Hi,
Even I am trying the browser close event for cross browser and this should trigger only when X button is clicked or page is refreshed. Here's the code pasted above which I am using but it only works in IE.
I know the issue is because of window.events and ClientX/ClientY.
Could someone please fix my code to make it work in all browsers? This is very urgent and have been striving since 3 days...

*Please Note: Current code is working in IE only and to check you have to close the browser window or refresh the page with mouse click

=====================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><head>
<title>Warning Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript">

// warning message javascript

var isOpera, isIE, isNav, isFox, isOther = false;
if (navigator.userAgent.indexOf("Opera") != -1) {
isOpera = true;
} else if (navigator.userAgent.indexOf("Firefox") != -1) {
isFox = true;
} else if (navigator.appName == "Microsoft Internet Explorer") {
isIE = true;
} else if (navigator.appName == "Netscape") {
isNav = true;
} else {
isOther = true;
}

if (isIE || isOpera || isNav || isFox || isOther) {
window.onbeforeunload = WarnUser;
function WarnUser() {
OffsetX = window.event.clientX;
OffsetY = window.event.clientY;
if (((window.event.clientX < 0) || (window.event.clientY < 0))
&& (isWarnUser == true)) {
event.returnValue = " ";
//window. önunload = sessionInvalidate;
} else {
// Reset the flag to its default value.
isWarnUser = true;
}
}
}




</script>

</head>
<body>
<script type="text/javascript">
var isWarnUser = true;
</script>
test
</body>
</html>
========================

ok, first of all i am not really sure what you want to do, but there is a event that occurs when you close the window.. and that is:

<BODY onUnload="function()">

if you want to check if the F5 button on the keyboard is pressed try something like:

document.onkeydown=keyaction;
function keyaction(e)
{
if(!e)e = window.event;
alert(e.keyCode);
switch(e.keyCode)
{
case 63:function()
}
}

i don;t remember what is the code for F5, try that code and check it out...
if you want to occur only if the X or refresh button is press, i don;t know how to do that...
another thing that you should know is that such a event when you are closing the window/site is annoying sometimes, at least the one you put in the test

ok, first of all i am not really sure what you want to do, but there is a event that occurs when you close the window.. and that is:

<BODY onUnload="function()">

Actually, this is not correct. There might bee a event named onunload (small-case is proffered after my knowledge), but the event onbeforeunload is triggered before that (I don't know the difference though, but my guessing is that onbeforeunload is triggered before the DOM-model is destroyed). Not saying that onunload is wrong, but it's not the only event triggered at browser close|redirect|refresh|++ At work we use onbeforeunload to save changes and it's confirmed working in all the browsers (though it's an ugly hack).

if you want to check if the F5 button on the keyboard is pressed try something like:

document.onkeydown=keyaction;
function keyaction(e)
{
if(!e)e = window.event;
alert(e.keyCode);
switch(e.keyCode)
{
case 63:function()
}
}

i don;t remember what is the code for F5, try that code and check it out...
if you want to occur only if the X or refresh button is press, i don;t know how to do that...
another thing that you should know is that such a event when you are closing the window/site is annoying sometimes, at least the one you put in the test

This might be true, it might be called when F5 is pressed, but I think this is browser-based, though not sure about that. Though what I do know is that handling onbeforeunload is somewhat difficult.

Here's what I'dd do:

<html>
<head>
<script language="javascript">
var isWarnUser = false;
function on_unload()
{
    if(isWarnUser)
        return " ";
    isWarnUser = true;
}
</script>
</head>
<body onbeforeunload="return on_unload();">.....</body>
</html>

Actually, this is not correct. There might bee a event named onunload (small-case is proffered after my knowledge), but the event onbeforeunload is triggered before that (I don't know the difference though, but my guessing is that onbeforeunload is triggered before the DOM-model is destroyed). Not saying that onunload is wrong, but it's not the only event triggered at browser close|redirect|refresh|++ At work we use onbeforeunload to save changes and it's confirmed working in all the browsers (though it's an ugly hack).

This might be true, it might be called when F5 is pressed, but I think this is browser-based, though not sure about that. Though what I do know is that handling onbeforeunload is somewhat difficult.

Here's what I'dd do:

<html>
<head>
<script language="javascript">
var isWarnUser = false;
function on_unload()
{
    if(isWarnUser)
        return " ";
    isWarnUser = true;
}
</script>
</head>
<body onbeforeunload="return on_unload();">.....</body>
</html>

Hi,

Nothing works out, please have a double check before delivering code.

Best,
Ay

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.