By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,373 Members | 1,763 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

remove onbeforeunload event

omerbutt
100+
P: 638
Hi
i am working on a chat module in Yii and implemented the window close prompter function as follows
Expand|Select|Wrap|Line Numbers
  1.   $(document).ready(function(){
  2.             // Initialise window close prompter
  3.             addEvent(window, \'load\', addListeners, true);
  4.   });
  5. /*WINDOW CLOSE PROMPTER CODE*/
  6.         // Cross browser event handling for IE 5+, NS6+ and Gecko
  7.         function addEvent(elm, evType, fn, useCapture)
  8.         {
  9.                 if (elm.addEventListener)
  10.                 {
  11.                         // Gecko
  12.                         elm.addEventListener(evType, fn, useCapture);
  13.                         return true;
  14.                 }
  15.                 else if (elm.attachEvent)
  16.                 {
  17.                         // Internet Explorer
  18.                         var r = elm.attachEvent(\'on\' + evType, fn);
  19.                         return r;
  20.                 }
  21.                 else
  22.                 {
  23.                         // netscape?
  24.                         elm[\'on\' + evType] = fn;
  25.                 }
  26.         }
  27.  
  28.         // Add Listeners
  29.         function addListeners(e)
  30.         {
  31.                 // Before unload listener
  32.                 addEvent(window, \'beforeunload\', exitAlert, false);
  33.         }
  34.  
  35.         // Exit Alert
  36.         function exitAlert(e)
  37.         {
  38.  
  39.             // default warning message
  40.                 var msg = "You will lose information if it has not already been saved.";
  41.  
  42.                 // set event
  43.                 if (!e) { e = window.event; }
  44.                 if (e) { e.returnValue = msg; }
  45.  
  46.                 // return warning message
  47.                 return msg;
  48.         }
  49.  
  50.  
now the basic purpose was to prompt the user if he accidentally clicks the close window / TAB button but it prompts me every time when i have to redirect the page for example , if i click endChatSession button it should logout and redirect to the review meeting form but when ever it wil redirect or reload it will call the same prompt, i thought if i could be able to remove / unbind the listener i would be able to achieve what i am trying to do, so i added the following line in the endChatSession function but it is not unbind / removing the listener.
Expand|Select|Wrap|Line Numbers
  1. window.removeEventListener('onload',addListeners,true);
  2.  
any help will be appreciated
regards,
Omer Aslam
Aug 7 '14 #1

✓ answered by omerbutt

No i wanted to prompt whenever user clicks accidently on the window or TAB close button but not when i intentionally want to redirect like in the logout method, i rewrote the whole scenario now it is done see below hope this helps some one out
Expand|Select|Wrap|Line Numbers
  1. $(document).ready(function(){
  2.             // Initialise window close prompter
  3.             window.onbeforeunload = function (evt) {
  4.                 var message = \'Are you sure you want to leave this page?\';
  5.                 if (typeof evt == \'undefined\') {
  6.                     evt = window.event;
  7.                 }
  8.                 if (evt ) {
  9.                     evt.returnValue = message;
  10.                 }
  11.                 return message;
  12.             }
  13.  
and then in the logout method i overwrite the method
Expand|Select|Wrap|Line Numbers
  1. window.onbeforeunload = function () {
  2.         // blank function do nothing
  3.       }
  4.  

Share this Question
Share on Google+
7 Replies


Dormilich
Expert Mod 5K+
P: 8,639
i thought if i could be able to remove / unbind the listener i would be able to achieve what i am trying to do, so i added the following line in the endChatSession function but it is not unbind / removing the listener.
thatís the wrong call, that would only remove the listener that is called onload not any listener defined elsewhere. you need to undo line #32.
Aug 7 '14 #2

omerbutt
100+
P: 638
Hope i am getting you write but then it wont be calling the prompt on close if i remove it.
regards,
Omer Aslam
Aug 7 '14 #3

Dormilich
Expert Mod 5K+
P: 8,639
isn’t that what you wanted?
Aug 7 '14 #4

omerbutt
100+
P: 638
No i wanted to prompt whenever user clicks accidently on the window or TAB close button but not when i intentionally want to redirect like in the logout method, i rewrote the whole scenario now it is done see below hope this helps some one out
Expand|Select|Wrap|Line Numbers
  1. $(document).ready(function(){
  2.             // Initialise window close prompter
  3.             window.onbeforeunload = function (evt) {
  4.                 var message = \'Are you sure you want to leave this page?\';
  5.                 if (typeof evt == \'undefined\') {
  6.                     evt = window.event;
  7.                 }
  8.                 if (evt ) {
  9.                     evt.returnValue = message;
  10.                 }
  11.                 return message;
  12.             }
  13.  
and then in the logout method i overwrite the method
Expand|Select|Wrap|Line Numbers
  1. window.onbeforeunload = function () {
  2.         // blank function do nothing
  3.       }
  4.  
Aug 7 '14 #5

omerbutt
100+
P: 638
the simplest one was rather the right thing to do. cheers.
regards,
Omer Aslam
Aug 7 '14 #6

Dormilich
Expert Mod 5K+
P: 8,639
just a question, when you immediately remove the beforeunload handler, why defining it in the first place?

besides that, what you have done is exactly what I said in post #2. only that you used onevent properties instead of addEventListener/removeEventListener.
Aug 7 '14 #7

omerbutt
100+
P: 638
no i dont immediately remove it the document.ready part run when chat loads, i actually am using it to prevent any accidental close of the chat window or tab , and the unloading part is actually a part of the logout method , where i throw an ajax call update tables and then in readyStateChange method redirect the page to the couch ratings form that the client has to fill up after his Text Chat appointment, at this point the prompt that i had loaded on the page load appears and halts the redirection, apart from it the appointment has a dedicated time frame after which the user would be automatically be logged out and redirected to the couch ratings form, hope i answered what you were asking.
regards,
Omer Aslam
Aug 9 '14 #8

Post your reply

Sign in to post your reply or Sign up for a free account.