944,055 Members | Top Members by Rank

Ad:
Nov 4th, 2009
0

Warning Message on Browser Close

Expand 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. ========================
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ayusman.mohanty is offline Offline
4 posts
since Nov 2009
Nov 4th, 2009
0
Re: Warning Message on Browser Close
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
Reputation Points: 10
Solved Threads: 2
Newbie Poster
futingkiller is offline Offline
17 posts
since Oct 2009
Nov 4th, 2009
0
Re: Warning Message on Browser Close
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).
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:
JavaScript Syntax (Toggle Plain Text)
  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 6:58 pm.
Reputation Points: 15
Solved Threads: 10
Junior Poster in Training
Alxandr is offline Offline
73 posts
since May 2009
Nov 5th, 2009
0
Re: Warning Message on Browser Close
Click to Expand / Collapse  Quote originally posted by Alxandr ...
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:
JavaScript Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ayusman.mohanty is offline Offline
4 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: How to Password protect files / sections on web page ?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: how can i make a window close itself after running the php code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC