2

How do I pass a variable like username between two jQueryMobile pages?

or two regular pages for that matter, having the variable as global does not work, since at the next include it will set the variable back to null.

How do I pass a variable a global variable between two html pages?

veeTrain
  • 2,915
  • 2
  • 26
  • 43
Astronaut
  • 6,691
  • 18
  • 61
  • 99
  • I thought that jQuery Mobile was all about single-page apps. – Pointy May 24 '12 at 14:05
  • 2
    @Pointy JQM pulls in the external page and attaches it to the DOM but it can be used for multi-page apps http://jquerymobile.com/demos/1.1.0/docs/pages/page-anatomy.html – Jack May 24 '12 at 14:11

4 Answers4

6

I usually go with local storage, you can store objects too. Ex:

var userInfo = {
            "username": "Bob",
            "roleName": "Admin",
            "image": "img/userPic.jpg",
        };

        //Store the object in local storage
        localStorage.setItem('loggedUser', JSON.stringify(userInfo));

then on the other page you can simply do:

var userInfo = JSON.parse(localStorage.getItem("loggedUser"));
var userName = userInfo.username;
Mario
  • 2,721
  • 1
  • 22
  • 23
  • I was thinking about using localStorage, I wanted to avoid having to look up localStorage, since I will have to use the same data a lot of times. I was wondering if appcache had a mechanism to store variables, since the application needs to work offline... localStorage is probably the simpler solution. Thanks. – Astronaut May 24 '12 at 21:58
1

You can pass it in the query string as long as you disable ajax on the hyperlink between the two pages.

<a href="nextPage.php?username=joe" data-ajax="false">Next page</a>

You miss out on the beneficial features of jQuery mobile transitions, but at least you can successfully pass variables between pages.

Update:

From the documentation:

Passing parameters between pages:

jQuery Mobile does not support query parameter passing to internal/embedded pages...

The documentation then goes on to suggest two plugins which I have not tried. Page params plugin and jquery mobile routing plugin.

Be sure to check out the documentation page linked above for their explanation on why passing parameters shouldn't work but if you load the page without the hashing (by disabling the ajax behavior) then you should be fine -- at least in my experience.

Community
  • 1
  • 1
veeTrain
  • 2,915
  • 2
  • 26
  • 43
  • Inside the same page they share the same context so there isn't an issue there. It's only wend I need to access the userID to perform queries to a websql database that the problem becomes evident. Ill try to look into the documentation. Thank you. – Astronaut May 24 '12 at 21:55
  • @AdamSurfari; I'm not sure what you mean; my answer (towards the beginning) refers to passing a username to a second page. I could be wrong, but I interpreted the documentation as meaning that all of your site pages are "internal" and everything on a different server would be "external". So by turning off the default behavior (`data-ajax="false"`) you'll be able to pass a parameter to a second page. Otherwise the second "internal" page will get loaded using the special jquery mobile framework where query parameters can get lost in the method described. – veeTrain May 25 '12 at 12:46
1

You can use either the URL through a query string or client side routing, you can use localStorage (assuming they are on the same domain), or you can use cookies.

If you decide to use client side routing there is a plug in specifically for JQM that ties in with the different JQM page events

https://github.com/azicchetti/jquerymobile-router

Jack
  • 10,943
  • 13
  • 50
  • 65
0

You may store variables for global usage the same way JQM stores its own configuration. Just create your own javascript object under $.mobile at 'mobileinit' and use it for your global variables:

    <!DOCTYPE html> 
    <html>
    <head>

        ...

        <script> // position before jquery.mobile.js is important for 'mobileinit' to fire
            $( document ).on( "mobileinit", function( event ) {

                //create global variable storage
                $.mobile.YourApplicationNameHere = {};

                //use it like this anywhere in your code:
                $.mobile.YourApplicationNameHere.globalVar1 = 1;
                $.mobile.YourApplicationNameHere.globalVar2 = 2;
                console.log("globalVar1 = " + $.mobile.YourApplicationNameHere.globalVar1);

            });
        </script>

        <script src="jquery/jquery.mobile-1.4.5.min.js"></script>

        ...

    </head>
        ...
    </html>

You may initialize your global variables anywhere, but 'mobileinit' is the earliest initialization point of JQM.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user318750
  • 326
  • 3
  • 7