473,466 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

remove onbeforeunload event

omerbutt
638 Contributor
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.  

7 12424
Dormilich
8,658 Recognized Expert Moderator Expert
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
638 Contributor
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
8,658 Recognized Expert Moderator Expert
isn’t that what you wanted?
Aug 7 '14 #4
omerbutt
638 Contributor
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
638 Contributor
the simplest one was rather the right thing to do. cheers.
regards,
Omer Aslam
Aug 7 '14 #6
Dormilich
8,658 Recognized Expert Moderator Expert
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
638 Contributor
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

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

Similar topics

6
by: Marcia Gulesian | last post by:
What code can I put in the 'onbeforeunload' event to cancel the 'unload' action (when user closes browser) and then present the user with an alert saying that he/she must use the logout button.
1
by: Tee | last post by:
Hi, Anyone know how to remove a event handler that inherited from another form? I have a base form, and there is a button with click event in the base form. 1 of my another form inherits this...
0
by: Rick Lubanovic | last post by:
I have been working on a script to check if a page has changed before the user leaves the page. I have a call to a javascript function on the onbeforeunload event. This seems to work in most...
1
by: RJN | last post by:
Hi I have an onbeforeunload event where in I call a server side script to release some locks on the databse. function releaseLock() { __doPostBack('btnUnlock',''); } <body...
1
by: RJN | last post by:
Hi I have a pop-up window which has a task menu on its left which are nothing but links to different screens. This window also has a close button. On click of close button I do some server side...
4
by: Charles Law | last post by:
Is there a way to dynamically remove an event handler from an event without knowing the name of the handler? For example, how can ClassB remove the handler without knowing the name, or how many...
1
by: lee_youjin | last post by:
I needed to defect when the user is clicking the IE X button ( top right). so I could execute the cleanup function. I was able to detect it using onbeforeunload event. Now I need to call the...
2
Frinavale
by: Frinavale | last post by:
I'm not sure what I'm doing wrong. I am attempting to configure my object so that the object's method is called during the onbeforeunload event of the page. I have it so that it calls the...
1
Frinavale
by: Frinavale | last post by:
I don't have a clue how to work around this problem and I'm open to any suggestions on the topic.... In Internet Explorer it appears that the onbeforeunload event is fireing despite the fact that...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.