5

What is the right way to pass parameters between pages in jquery mobile. In Q&A of jquery mobile, there is some suggestion for plugin. Is it mandatory? Please let me know the correct way. There is no one concrete answer. I have to pass parameters for all links in my page.

http://view.jquerymobile.com/master/demos/faq/pass-query-params-to-page.php

Gajotres
  • 57,309
  • 16
  • 102
  • 130
user694688
  • 593
  • 1
  • 15
  • 32
  • See also this answer to a similar question: http://stackoverflow.com/a/35259962/3209859 – Kzar Feb 07 '16 at 22:39

2 Answers2

19

Data/Parameters manipulation between page transitions

It is possible to send a parameter/s from one page to another during page transition. It can be done in few ways.

Reference: https://stackoverflow.com/a/13932240/1848600

Solution 1:

You can pass values with changePage:

$.mobile.changePage('page2.html', { dataUrl : "page2.html?paremeter=123", data : { 'paremeter' : '123' }, reloadPage : true, changeHash : true });

And read them like this:

$(document).on('pagebeforeshow', "#index", function (event, data) {
    var parameters = $(this).data("url").split("?")[1];;
    parameter = parameters.replace("parameter=","");  
    alert(parameter);
});

[Example][3]:

index.html

<!DOCTYPE html>
  <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="widdiv=device-widdiv, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <title>
    </title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js">
    </script>
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>  
    <script>
        $(document).on('pagebeforeshow', "#index",function () {
            $(document).on('click', "#changePage",function () {     
                $.mobile.changePage('second.html', { dataUrl : "second.html?paremeter=123", data : { 'paremeter' : '123' }, reloadPage : false, changeHash : true });
            }); 
        }); 

        $(document).on('pagebeforeshow', "#second",function () {
            var parameters = $(this).data("url").split("?")[1];;
            parameter = parameters.replace("parameter=","");  
            alert(parameter);
        });         
    </script>
   </head>
   <body>
    <!-- Home -->
    <div data-role="page" id="index">
        <div data-role="header">
            <h3>
                First Page
            </h3>
        </div>
        <div data-role="content">
          <a data-role="button" id="changePage">Test</a>
        </div> <!--content-->
    </div><!--page-->

  </body>
</html>

second.html

<!DOCTYPE html>
  <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="widdiv=device-widdiv, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <title>
    </title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js">
    </script>
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>  
   </head>
   <body>
    <!-- Home -->
    <div data-role="page" id="second">
        <div data-role="header">
            <h3>
                Second Page
            </h3>
        </div>
        <div data-role="content">

        </div> <!--content-->
    </div><!--page-->

  </body>
</html>

Solution 2:

Or you can create a persistent javascript object for a storage purpose. As long ajax is used for page loading (and page is not reloaded in any way) that object will stay active.

var storeObject = {
    firstname : '',
    lastname : ''
}

Example: http://jsfiddle.net/Gajotres/9KKbx/

Solution 3:

You can also access data from the previous page like this:

$('#index').live('pagebeforeshow', function (e, data) {
    alert(data.prevPage.attr('id'));
});   

prevPage object holds a complete previous page.

Solution 4:

As a last solution we have a nifty HTML implementation of localStorage. It only works with HTML5 browsers (including Android and iOS browsers) but all stored data is persistent through page refresh.

if(typeof(Storage)!=="undefined") {
    localStorage.firstname="Dragan";
    localStorage.lastname="Gaic";            
}

Example: http://jsfiddle.net/Gajotres/J9NTr/

More info

If you want to learn more about this topic take a look at this article. You will find several solutions with examples.

Community
  • 1
  • 1
Gajotres
  • 57,309
  • 16
  • 102
  • 130
  • Thanks Gajotres. There are so many ways to do the same thing. But, which is the standard recommended way. My simple example, I have a list of news articles. When some news article is tapped, i want to pass the corresponding news article id to the server so that it opens the news details page. Can you please show this in an example. Say, i have a
    News Listing
    -News Listing here refers to list of items.
    – user694688 May 13 '13 at 10:23
  • That is something different. My solutions are made to work without server support. Solution 1 would work in your case but that is not a standard way. Correct way would be to use a ajax, connect to server, pull new page data and show it in another page. Here's a working example: http://stackoverflow.com/questions/15205437/jquery-mobile-how-to-correctly-submit-form-data/15205612#15205612 – Gajotres May 13 '13 at 10:34
  • Incase if i have passed a parameter to a page, and retrieved using the data-url attribute, it works fine. But, if i refresh that page, the url parameter will not be available. What should be done for this. – user694688 Jun 08 '13 at 09:38
  • Gajotres - Can you pls clarify the above question. – user694688 Jun 11 '13 at 08:35
  • @user694688 I created a [plug-in](https://github.com/CameronAskew/jquery.mobile.paramsHandler) which preserves the variables in the actual URL string so refreshes and going going directly to a page work fine. – Cameron Askew Apr 03 '14 at 06:14
  • Why read the passed parameter from the page that's sending it? Shouldn't the new page read the parameter that's been passed from a previous one? – Joon. P May 02 '16 at 14:28
3

You can also use it with external relations in links (for me and my purpose its easier), so I wanted to share this:

HTML:

 <a href="page1.htm?structure='123'">Structure</a>

JS:

$( document ).on( "pageinit", "#page1", function( event ) {
  var parameters = $(this).data("url").split("?")[1];
  parameter = parameters.replace("structure=","");
  alert(parameter);
});

In my case, the structure='123' is created dynamically, nobody writes a fixed value '123' for dynamic purposes :-)

Thanks for the URL-splitting of the above post (pls. favor his post), this is just an addition / further explanation.

dotchuZ
  • 2,621
  • 11
  • 39
  • 65