Calling a method that's part of an object during the onbeforeunload event.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    Calling a method that's part of an object during the onbeforeunload event.

    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 method during the begin request of an Ajax call to the server...I don't know why I can't get the onbeforeunload event to work

    Here's what I have (I'm using the Ajax.NET library):
    Code:
    MyNamespace.MyClass = function(element) {
        MyNamespace.MyClass.initializeBase(this, [element]);
    
        this._onBeginRequestHandler = Function.createDelegate(this, this._onBeginRequest);
        this._onEndRequestHandler = Function.createDelegate(this, this._onEndRequest);
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_beginRequest(this._onBeginRequestHandler);
        prm.add_endRequest(this._onEndRequestHandler);
    
    // Here is what is not working
            window.onbeforeunload = this._myMethod;
    //I have also tried the following but it doesn't work either:
    //      window.onbeforeunload = this._onBeginRequestHandler;
    //since the _onBeginRequestHandler method just calls _myMethod anyways.
    
    }
    Thanks,

    -Frinny
  • phvfl
    Recognized Expert New Member
    • Aug 2007
    • 173

    #2
    What does _myMethod return. The OnBeforeUnload event is usually used by assigning a string value to the event.returnVal ue property which is then displayed in an exit page prompt. So the following would show a dialog with the message "Page close prompt"
    Code:
    function pageClosing(e){
      // For most browsers
      e = e || window.event;
    
      // Ensure that the event object is not null
      if(e){
        e.returnValue = 'Page close prompt';
      }
    
      // For browsers that don't support the returnValue property.
      return 'Page close prompt';
    }
    
    window.onbeforeunload = pageClosing;
    The prompt typically has an OK and Cancel button - OK exits the page and Cancel prevents the window closing. The prompt can not be interacted with from within script.

    If you perform an AJAX request within the function it should start but the result may not be processed if the page is exited.

    Hope this helps

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Thanks for the info but I already have a good idea about how the onbeforeunload event works ;)

      The method does not return anything so the Ok/Cancel prompt is not displayed....th e method displays 2 <div> elements that prevent the user from accessing the controls on the page while a request to the server is being processed.

      I'm not sure why I can't seem to call the method that belongs to the object during the onbeforeunload event....

      -Frinny

      Comment

      Working...