6

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 redirect user to the login page if the session is dead?

Ville Salonen
  • 2,654
  • 4
  • 29
  • 34

4 Answers4

3

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

Peter
  • 47,963
  • 46
  • 132
  • 181
  • Yes, this is available in the recent versions. However I had this problem back in September when this wasn't implemented yet. In my own answer I've described the pageloadfailed event. Apparently pagechangefailed might suffice as well. – Ville Salonen Apr 08 '12 at 06:15
2

Related:

You could use something like

pagebeforeshow

Triggered on the page being shown, before its transition begins.

Example (pseudo code):

$('#pageId').live('pagebeforeshow',function(event, ui){
    // check session here
    if(!$session) {
        // redirect to login
        $.mobile.changePage('#login');
    }
});
Community
  • 1
  • 1
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
  • This is a bit better solution. However this still feels a bit overkill. I was thinking about doing a fork of jQuery Mobile and adding a subscribable error event so I could handle the error only when it happens. Then again premature optimization is hazardous so maybe I'll roll with your suggestion for now. :) – Ville Salonen Sep 20 '11 at 17:19
  • This didn't help after all. pagebeforeshow and pagebeforehide events aren't triggered if the page loading fails. – Ville Salonen Sep 21 '11 at 07:37
  • My next suggestion would be to use AJAX and check the Apache response, Example: if you get a 200 response you're good, else ... might need to map more responses as well as this is just a concept – Phill Pafford Sep 21 '11 at 13:08
  • Or have the AJAX request the page and have the back end (like PHP) validate the Session, if not valid use PHP to send the URL desired as the response. Example: AJAX request secure.php page, PHP checks session, if session is good return secure.php else return login.php – Phill Pafford Sep 21 '11 at 13:11
1

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.

Ville Salonen
  • 2,654
  • 4
  • 29
  • 34
0
$(a).click(function(){
    // check for session?
});
Chris G.
  • 3,963
  • 2
  • 21
  • 40
  • That's one way to solve it but it might be a bit excessive. That would (about) double the amount of requests and as the application is used mainly on mobile devices, that might become a noticeable problem. – Ville Salonen Sep 20 '11 at 12:30