This is a migrated thread and some comments may be shown as answers.

jquery .bind on ajax event

2 Answers 112 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Fabio
Top achievements
Rank 2
Fabio asked on 21 Dec 2010, 06:11 PM
Hi

i have a .js file to control when a user close the browser tab then destroy all session on the server.
This control when not fire when change the page with a link 'a' or submit a form else the control will fire.

I have a listbox ajaxed and when i click an item my cotrol fire and destroy all session... disaster...

its possible to bind all ajax avent for block the destroy session?

my js.file
======================================
var validNavigation = false;

/* No HTML output is returned*/
function endSession() {
    //$.get("../logout.aspx");
    alert("MedOFFICEweb - Sessione terminata.");
    validNavigation = false
}

function wireUpEvents() {

    /*
    * For a list of events that triggers onbeforeunload on IE
    * check http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx
    */

    window.onbeforeunload = function() {       
            if (!validNavigation) {
                endSession();
            }                                  
    }

 
        
    // Attach the event click for all links in the page
    $("a").bind("click", function() {
    validNavigation = true;
        //alert("a")
    });

    // Attach the event submit for all forms in the page
    $("form").bind("submit", function() {
    validNavigation = true;
        //alert("form")
    });

 }

// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {    
    wireUpEvents();
});
===========================================



 

2 Answers, 1 is accepted

Sort by
0
Bodevain Svensson
Top achievements
Rank 1
answered on 22 Dec 2010, 12:51 PM
I suspect that the window.onbeforeunload handler is fired on ajax request and the conditional block which clears the session is entered. Is this true? If so, you need an additional check to see whether ajax request is in progress. This can be done via the PageRequestManager instance as follows:

window.onbeforeunload = function() {  
            var prm = Sys.WebForms.PageRequestManager.getInstance();     
            if ((!validNavigation) || (!prm.get_isInAsyncPostBack())) {
                endSession();
            }                                  
    }

Details about the PageRequestManager class and its members are available in MSDN:
http://msdn.microsoft.com/en-us/library/bb311028.aspx

Bodevain
0
Fabio
Top achievements
Rank 2
answered on 23 Dec 2010, 06:12 PM
Yes its true, but prm.get_isInAsyncPostBack() its false in all case... the session will terminate :-(

exist another client check with JS or Jquery to intercept the browser/tabs close?

THX
Tags
Ajax
Asked by
Fabio
Top achievements
Rank 2
Answers by
Bodevain Svensson
Top achievements
Rank 1
Fabio
Top achievements
Rank 2
Share this question
or