How to handle page change errors in jQuery Mobile
I'm writing a jQuery Mobile application that requires user authentication. The same user cannot have her session open from multiple locations: if user logins from another browser, the previous session is marked as dead.
If the user attempts to move to another page with the browser with the dead session, "Error loading page" message is displayed. This is bad because user might not know why she's getting this error. Is it possible to tap in to the error event so I could check for the status of the session and r开发者_运维知识库edirect user to the login page if the session is dead?
How about the
pagechangefailed
event?
It is fired when the pageload fails, which seems to be the case.
More info on http://jquerymobile.com/test/docs/api/events.html
Related:
- Ajax - I need to check whether a valid session exists before every AJAX requests
- How to manage a redirect request after a jQuery Ajax call
You could use something like
pagebeforeshow
Triggered on the page being shown, before its transition begins.
- http://jquerymobile.com/demos/1.0b3/docs/api/events.html
Example (pseudo code):
$('#pageId').live('pagebeforeshow',function(event, ui){
// check session here
if(!$session) {
// redirect to login
$.mobile.changePage('#login');
}
});
I ended up forking jQuery Mobile and adding an ability to add custom error handler: https://github.com/jquery/jquery-mobile/pull/2504 I think this superior to the other suggestions because it doesn't add any overhead except when the error actually occurs.
UPDATE: There will be a new pageloadfailed event in jQuery Mobile RC1. That will solve this problem elegantly and according to project standards.
$(a).click(function(){
// check for session?
});
精彩评论