My jQuery Mobile app consists of a single index.html
page and contains only one page with a link on startup:
Here is my method for dynamically adding content to my Jquery Mobile websites:
First I create a "wrapper" data-role=page div like so:
Next I load data from an external source into a div tag located in my "wrapper" page:
function my_data_loading_function(page) {
if ($('#' + page + '-content').is(':empty')) {
$.mobile.pageLoading();
$('#' + page + '-content').load("my_external_script.php", function() {
$.mobile.pageLoading(true);
$('#' + page + '-content ul').listview();
$('#' + page + '-content ul').page();
});
}
}
Some Notes:
$.mobile.pageLoading(); and $.mobile.pageLoading(true); show and hide (respectively) the Jquery Mobile loading spinner.
if ($('#' + page + '-content').is(':empty')) { allows the user to navaigate away from the dynamically created page and then come back and not have to re-load the data until a full page refresh occurs.
My dynamically created page included mostly a list so listview() makes the jquery mobile framework refresh the list selected to add the proper css, page() does the same to other page elements; however you may only need to use one or the other depending on your content (or none at all if its just plain text).
I realize this isn't creating a whole page dynamically because the "wrapper" page is already hard-coded but if you want to add a whole new page you can probably use something like: (untested)
$(document).append('FOO BAR');
$('#my_page_id').page();
If you really want to make it all dynamically created you can check for window.location.hash and create your data-role=page div with the id set as the window.location.hash.
Also I am using Jquery 1.6 and Jquery Mobile 1.0a4.1
I hope something in there can help someone out there :)