Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 781
Ville Salonen Avatar asked Sep 20 '11 12:09

Ville Salonen


People also ask

What is changepage in jQuery Mobile?

Description: Programmatically change from one page to another. Note: jQuery.mobile.changePage is deprecated as of jQuery Mobile 1.4.0 and will be removed in 1.5.0. Use the pagecontainer widget's change () method instead.

How do you handle Ajax errors?

A common error is when AJAX returns no data. This can be handled by adding error messages, see the following example of an AJAX contact form. Checking in firebug the has a StatusText field which can be used to determine the type of jQuery error.

What is the default setting for changepage?

By default, changePage () ignores requests to change to the current active page. Setting this option to true, allows the request to execute. Developers should note that some of the page transitions assume that the fromPage and toPage of a changePage request are different, so they may not animate as expected.

When to use the changepage () method?

Used only when the 'to' argument of changePage () is a URL. Programmatically change from one page to another. This method is used internally for the page loading and transitioning that occurs as a result of clicking a link or submitting a form, when those features are enabled.


2 Answers

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

like image 129
Peter Avatar answered Oct 02 '22 13:10

Peter


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');
    }
});
like image 39
Phill Pafford Avatar answered Oct 02 '22 13:10

Phill Pafford