Warning Message on Browser Close

Please support our JavaScript / DHTML / AJAX advertiser: $6.99 Domain Names at 1&1. Includes Free Privacy. Save Now!
Reply

Join Date: Nov 2009
Posts: 4
Reputation: ayusman.mohanty is an unknown quantity at this point 
Solved Threads: 0
ayusman.mohanty ayusman.mohanty is offline Offline
Newbie Poster

Warning Message on Browser Close

 
0
  #1
Nov 4th, 2009
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
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. =====================
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><head>
  3. <title>Warning Test</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <meta http-equiv="Content-Style-Type" content="text/css">
  6. <meta http-equiv="Content-Script-Type" content="text/javascript">
  7. <script language="JavaScript">
  8.  
  9. // warning message javascript
  10.  
  11. var isOpera, isIE, isNav, isFox, isOther = false;
  12. if (navigator.userAgent.indexOf("Opera") != -1) {
  13. isOpera = true;
  14. } else if (navigator.userAgent.indexOf("Firefox") != -1) {
  15. isFox = true;
  16. } else if (navigator.appName == "Microsoft Internet Explorer") {
  17. isIE = true;
  18. } else if (navigator.appName == "Netscape") {
  19. isNav = true;
  20. } else {
  21. isOther = true;
  22. }
  23.  
  24. if (isIE || isOpera || isNav || isFox || isOther) {
  25. window.onbeforeunload = WarnUser;
  26. function WarnUser() {
  27. OffsetX = window.event.clientX;
  28. OffsetY = window.event.clientY;
  29. if (((window.event.clientX < 0) || (window.event.clientY < 0))
  30. && (isWarnUser == true)) {
  31. event.returnValue = " ";
  32. //window. önunload = sessionInvalidate;
  33. } else {
  34. // Reset the flag to its default value.
  35. isWarnUser = true;
  36. }
  37. }
  38. }
  39.  
  40.  
  41.  
  42.  
  43. </script>
  44.  
  45. </head>
  46. <body>
  47. <script type="text/javascript">
  48. var isWarnUser = true;
  49. </script>
  50. test
  51. </body>
  52. </html>
  53. ========================
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: futingkiller is an unknown quantity at this point 
Solved Threads: 2
futingkiller futingkiller is offline Offline
Newbie Poster
 
0
  #2
Nov 4th, 2009
Originally Posted by ayusman.mohanty View Post
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
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. =====================
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><head>
  3. <title>Warning Test</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <meta http-equiv="Content-Style-Type" content="text/css">
  6. <meta http-equiv="Content-Script-Type" content="text/javascript">
  7. <script language="JavaScript">
  8.  
  9. // warning message javascript
  10.  
  11. var isOpera, isIE, isNav, isFox, isOther = false;
  12. if (navigator.userAgent.indexOf("Opera") != -1) {
  13. isOpera = true;
  14. } else if (navigator.userAgent.indexOf("Firefox") != -1) {
  15. isFox = true;
  16. } else if (navigator.appName == "Microsoft Internet Explorer") {
  17. isIE = true;
  18. } else if (navigator.appName == "Netscape") {
  19. isNav = true;
  20. } else {
  21. isOther = true;
  22. }
  23.  
  24. if (isIE || isOpera || isNav || isFox || isOther) {
  25. window.onbeforeunload = WarnUser;
  26. function WarnUser() {
  27. OffsetX = window.event.clientX;
  28. OffsetY = window.event.clientY;
  29. if (((window.event.clientX < 0) || (window.event.clientY < 0))
  30. && (isWarnUser == true)) {
  31. event.returnValue = " ";
  32. //window. önunload = sessionInvalidate;
  33. } else {
  34. // Reset the flag to its default value.
  35. isWarnUser = true;
  36. }
  37. }
  38. }
  39.  
  40.  
  41.  
  42.  
  43. </script>
  44.  
  45. </head>
  46. <body>
  47. <script type="text/javascript">
  48. var isWarnUser = true;
  49. </script>
  50. test
  51. </body>
  52. </html>
  53. ========================
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <BODY onUnload="function()">
if you want to check if the F5 button on the keyboard is pressed try something like:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.onkeydown=keyaction;
  2. function keyaction(e)
  3. {
  4. if(!e)e = window.event;
  5. alert(e.keyCode);
  6. switch(e.keyCode)
  7. {
  8. case 63:function()
  9. }
  10. }
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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 73
Reputation: Alxandr is an unknown quantity at this point 
Solved Threads: 9
Alxandr's Avatar
Alxandr Alxandr is offline Offline
Junior Poster in Training
 
0
  #3
Nov 4th, 2009
Originally Posted by futingkiller View Post
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <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).
Originally Posted by futingkiller View Post
if you want to check if the F5 button on the keyboard is pressed try something like:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.onkeydown=keyaction;
  2. function keyaction(e)
  3. {
  4. if(!e)e = window.event;
  5. alert(e.keyCode);
  6. switch(e.keyCode)
  7. {
  8. case 63:function()
  9. }
  10. }
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:
  1. <html>
  2. <head>
  3. <script language="javascript">
  4. var isWarnUser = false;
  5. function on_unload()
  6. {
  7. if(isWarnUser)
  8. return " ";
  9. isWarnUser = true;
  10. }
  11. </script>
  12. </head>
  13. <body onbeforeunload="return on_unload();">.....</body>
  14. </html>
Last edited by Alxandr; Nov 4th, 2009 at 5:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: ayusman.mohanty is an unknown quantity at this point 
Solved Threads: 0
ayusman.mohanty ayusman.mohanty is offline Offline
Newbie Poster
 
0
  #4
Nov 5th, 2009
Originally Posted by Alxandr View Post
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:
  1. <html>
  2. <head>
  3. <script language="javascript">
  4. var isWarnUser = false;
  5. function on_unload()
  6. {
  7. if(isWarnUser)
  8. return " ";
  9. isWarnUser = true;
  10. }
  11. </script>
  12. </head>
  13. <body onbeforeunload="return on_unload();">.....</body>
  14. </html>
Hi,

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

Best,
Ay
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 952 | Replies: 3
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC