Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Mobile 301 Redirect Issues

I am using jQuery 1.6.4 with jQuery Mobile 1.0.1. I am running into an issue anytime you link to a page that then tries to do a 301 redirect.

I've setup a sample page at: http://www.widgetsandburritos.com/jquery-mobile-test/

The only thing on this page is the jQuery Mobile includes and a link to another page that has a 301 redirect somewhere else.

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
        <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
    </head>
    <body>
        <a href="301test.php">301 test</a>    
    </body>
</html>

301test.php has the following content:

<?php
header( "HTTP/1.1 301 Moved Permanently" ); 
header( "Location: 301success.html" ); 
?>

This should just simply pass the browser to 301success.html. It works if you directly go to that URL

http://www.widgetsandburritos.com/jquery-mobile-test/301test.php

But when you click on the link from the page using jQuery Mobile, it shows "undefined" instead. Is jQuery Mobile currently incapable of handling redirects?

Any possible work arounds?

Thanks for your help.

EDIT [3/23/12 12:41AM CST]

I also posted this problem on the jQuery Mobile forums. Somebody there recommended adding rel="external" to the anchor tag. This technically works if all you are doing is making a link, but won't fix the issue if you get to the redirect via some other mechanism, such as a POST request.

To illustrate, I've setup a secondary test at http://www.widgetsandburritos.com/jquery-mobile-test/test2.html

<!DOCTYPE html>
<html>
        <head>
                <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
                <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
                <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
        </head>
        <body>

                <form method="post" action="301test.php">
                        <input type="submit" value="test" />
                </form>

        </body>
</html>

Instead of arriving at the 301test.php redirect page from a link, it's now the location of a form we're submitting to. The context this would be used, would be such that if you submit a form with errors, it would stay on the same page allowing you to correct the errors. If there were no errors, it redirects you to a success page. This is done to avoid submitting the form again if a user refreshes their browser. It works brilliantly in normal web applications. But in combo with jQuery Mobile it doesn't seem to work.

Just thought I'd give some additional context to anyone else following this issue.

like image 384
David Stinemetze Avatar asked Mar 22 '12 19:03

David Stinemetze


2 Answers

Figured out the answer to my own problem. In the above, I mentioned that this was causing problems using the <form> tag. After browsing through the jQuery Mobile documentation I found this page: http://jquerymobile.com/test/docs/forms/forms-sample.html

The trick here is if you're doing a form, to force it to not use AJAX. You do this by adding

data-ajax="false" to the FORM tag.

So this changes

<form method="post" action="301test.php">

to

<form method="post" action="301test.php" data-ajax="false">

And just to reiterate what was said above. If you need to do something with an anchor link, just add rel="external" to it.

So this changes

<a href="301test.php">301 test</a> 

to

<a href="301test.php" rel="external">301 test</a> 
like image 130
David Stinemetze Avatar answered Oct 18 '22 06:10

David Stinemetze


The issue is deeper. Take a look here or here.

It seems that XMLHttpRequest object (the one used for doing AJAX requests) handles redirects on its own and returns the final response. Which means that jQuery Mobile can't know that it should update the URL.

The solution is to use the data-url attribute on the final page. It forces jQuery Mobile to update the URL in the browser. Kind of a workaround but far from being a hack.

By the way there are more issues with jQuery Mobile, AJAX and redirects - for instance if you click the browser's back button after an AJAX-redirect, jQuery Mobile (up till 1.1) might produce a final page under the URL of the redirecting page. Therefore using data-ajax="false" is a wise choice.

EDIT:

But even data-ajax="false" is not a bullet-proof solution. Using it splits your mobile app into multiple browser pages, which brings all sorts of browser differences to the party. For instance Firefox has so called bf cache whereas Chrome doesn't. This is an unholy mess and I'm starting to think that something like Sencha Touch is much better suited for developing pages that pretend to be mobile apps.

EDIT 2:

Alternatively, one could avoid regular form submissions and use own AJAX code for that and then switch pages based on the result, but I cannot resist thinking that it's 2012 and such things should automated and work flawlessly without sweating.

like image 33
Tomasz Zieliński Avatar answered Oct 18 '22 08:10

Tomasz Zieliński