1

I'm doing an ios web app using Cordova and JQuery. I create the index.html which has a 3-row list view. when the row is clicked, the page will change to another page.html. According to the clicked row, the JS will ask data from the server and refresh the page.html. Now My Question is what is the best practice to implement it? I mean which is the regular style to do it as I'm fresh to the front-end. Currently I add the anchor in index.html:

<li><a href="page.html">A Header Bars</a></li>

However, I meet problems:

  1. I don't know how to get the clicked row information in page.html
  2. In page.html, how I can start a request to fetch the data? In page.html, I write:

    $(document).ready(function()
    {
        console.log("test");
        document.write("page test");
    })
    

But it is not called.

Thanks.

scorpiozj
  • 2,687
  • 5
  • 34
  • 60

3 Answers3

6

If you want a value from one page to another you have few options (same rules apply for single jQM html with multiple page's and for jQM project built around multiple html files):

I. On the second page use pagebeforeshow and retrieve all needed data through a data object. Lets say you have 2 html files, first html has an id "page1" and second one has an id "page2"), example:

$('#page2').live('pagebeforeshow', function (e, data) {
    alert(data.prevPage.find('div[data-role="content"]').attr('id'));
});

II. Second option is to create a shared object which will be used as data storage:

var storeObject = {
    someValue : '1',
    anotherValue : '2'
}

This is an easiest solution but it will only work while ajax page loading is active.

III. 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:

$('#page2').live('pagebeforeshow', function (e, data) {
    var paremeter = $(this).data("url").split("?")[1];;
    paremeter = paremeter.replace("paremeter=","");   
    alert(paremeter);
});

More info

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

Gajotres
  • 57,309
  • 16
  • 102
  • 130
  • I put the first script in the head/body of page2.html using – scorpiozj Dec 20 '12 at 02:49
  • It looks like the url variables are not maintained in the URL however, is that correct? So going directly to a page or refreshing a page won't work well...I created a [plug-in](https://github.com/CameronAskew/jquery.mobile.paramsHandler) which allows for URL parameters and maintains them in the URL. – Cameron Askew Apr 03 '14 at 06:11
0

check this example here, Hope it will help

http://wpcertification.blogspot.com/2012/05/using-jquery-mobile-in-phonegapcordova.html

Saifuddin Sarker
  • 853
  • 3
  • 8
  • 26
0

https://stackoverflow.com/a/16497284/3169868 answers this well. For cordova+wp8, the option with LocalStorage is easy to implement.

Community
  • 1
  • 1
S_S
  • 1,276
  • 4
  • 24
  • 47