remove onbeforeunload event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    remove onbeforeunload event

    Hi
    i am working on a chat module in Yii and implemented the window close prompter function as follows
    [code=javascript]
    $(document).rea dy(function(){
    // Initialise window close prompter
    addEvent(window , \'load\', addListeners, true);
    });
    /*WINDOW CLOSE PROMPTER CODE*/
    // Cross browser event handling for IE 5+, NS6+ and Gecko
    function addEvent(elm, evType, fn, useCapture)
    {
    if (elm.addEventLi stener)
    {
    // Gecko
    elm.addEventLis tener(evType, fn, useCapture);
    return true;
    }
    else if (elm.attachEven t)
    {
    // Internet Explorer
    var r = elm.attachEvent (\'on\' + evType, fn);
    return r;
    }
    else
    {
    // netscape?
    elm[\'on\' + evType] = fn;
    }
    }

    // Add Listeners
    function addListeners(e)
    {
    // Before unload listener
    addEvent(window , \'beforeunload\ ', exitAlert, false);
    }

    // Exit Alert
    function exitAlert(e)
    {

    // default warning message
    var msg = "You will lose information if it has not already been saved.";

    // set event
    if (!e) { e = window.event; }
    if (e) { e.returnValue = msg; }

    // return warning message
    return msg;
    }

    [/code]
    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.
    [code=javascript]
    window.removeEv entListener('on load',addListen ers,true);
    [/code]

    any help will be appreciated
    regards,
    Omer Aslam
    Last edited by omerbutt; Aug 7 '14, 01:29 PM. Reason: typo
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.

    Comment

    • omerbutt
      Contributor
      • Nov 2006
      • 638

      #3
      Hope i am getting you write but then it wont be calling the prompt on close if i remove it.
      regards,
      Omer Aslam

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        isn’t that what you wanted?

        Comment

        • omerbutt
          Contributor
          • Nov 2006
          • 638

          #5
          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
          [code=javascript]
          $(document).rea dy(function(){
          // Initialise window close prompter
          window.onbefore unload = function (evt) {
          var message = \'Are you sure you want to leave this page?\';
          if (typeof evt == \'undefined\') {
          evt = window.event;
          }
          if (evt ) {
          evt.returnValue = message;
          }
          return message;
          }
          [/code]
          and then in the logout method i overwrite the method
          [code=javascript]
          window.onbefore unload = function () {
          // blank function do nothing
          }
          [/code]

          Comment

          • omerbutt
            Contributor
            • Nov 2006
            • 638

            #6
            the simplest one was rather the right thing to do. cheers.
            regards,
            Omer Aslam

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              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 addEventListene r/removeEventList ener.

              Comment

              • omerbutt
                Contributor
                • Nov 2006
                • 638

                #8
                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 readyStateChang e 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

                Comment

                Working...