Take the 2-minute tour ×

I am creating a simple vocabulary page (currently for Korean-English). All the cards are saved in localStorage. I am using jQueryMobile with multiple page template as shown in code below:

    <meta charset="utf-8">
    <title>Card Reader</title>

    <link rel="stylesheet"  href="jquery.mobile/jquery.mobile-1.1.0.css" />
    <link rel="stylesheet" href="docs/assets/css/jqm-docs.css" />
    <link rel="stylesheet" href="docsdemos-style-override.css" />
    <script type="text/javascript" src="custom_codes.js"></script>
    <script type="text/javascript" src="jquery.mobile/jquery-1.7.2.min"></script>
    <script type="text/javascript" src="jquery.mobile/jquery.mobile-1.1.0.js"></script>
    var list = JSON.parse(localStorage.getItem('cardList'));
        list = JSON.parse(localStorage.getItem('cardList'));                
    $.each(list,function(i, value){
        $newItem = $('<li />');                                                                     
        $link = $('<a data-transition="slidefade" href="#wordListPage">'+value+'</a>');

function loadWordList(cardName,cardId){
    var wordList = JSON.parse(localStorage.getItem(cardId));
        alert('no words in the list');              
    $list = $('<ul/>');
    $.each(wordList,function(i, value){


<div data-role="page" id="welcomePage">
        Welcome to Korean Learner
    <a data-transition="slidefade" href="#cardsListPage">           
        Load Cards          

<div data-role="page" id="cardsListPage">
    <div data-role="content">
        <ul id="cardList" data-role="listview">

<div data-role="page" id="wordListPage">
    <div data-role="header" class="ui-header ui-bar-b" data-position="fixed">
        <a data-rel="back">Back</a>
        <h1 id="cardTitle" >Card List</h1>
    <div data-role="content" id="wordList">         


When I check this in browser, output flow is as in image below:

Link to image, as I cannot post here being new user

I have used javascript to load cardList and wordlist. populateCards() funtion is a function in custom_codes.js which only loads an array of cards into localstorage. The only error I am getting right now is that wordList page loads without CSS in first load (third shot in flow) and is perfectly fine after I come back (last shot). Can anyone help me fix this error. Also I would be happy to get some performance tips as I am targeting this to use in mobilephones.

Thank you in advance.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Welcome to jQM, unlike jQuery you don't use $(document).ready anymore for starters (http://jquerymobile.com/test/docs/api/events.html) you now listen for the pageinit and pageshow event, likely your issue is that your JS is running after jQM fires pageshow which fires immediately after the pageinit

pageshow re-fires when you navigate back to a page, that's why it is working when you go back

When jQM fires pageshow that's when it goes through and applies any CSS that you're using from its API. But when that fired, you never added the elements yet, so they didn't get styled by jQM.

If the elements are added after through an async call then you need to manually call the .listview('refresh') too I think.

I also have an older answer where I outline how I organized my JS to be easily understandable/debuggable in a multi-page environment how to organize ur jQM JS

Also you are using a single-page template if welcomePage and cardsListPage are in the same file.

Multi-page template is when you have each of those pages in a separate file, which I think is better since you're loading less data at the start if you're concerned about performance. But you need to follow the JS setup I have in my link or your own homegrown solution, but you're basically trying to load all your JS once regardless of which page you start from. And handle for each pageinit/pageshow events and have some way of determining which page of your multi-page templates your on.

You're using ids but that doesn't work well because in a multi-page template jQM may load any page more than once (into a single DOM, check the debugger to see - assuming domCaching is on), well I guess you still know what page it is by checking .attr('id') but it's misleading since if you did a $('#cardsListPage') you may get one of many elements.

Hope that gets you off to a start.

share|improve this answer
Thanks a lot :) , I'll try as you suggested and hope it works. –  Sunil Nov 19 '12 at 0:59
add comment

Your Answer


