With today's dynamic systems getting them to load properly is always a challenge. Especially if you have content loaded via Ajax after the initial load. This can be the hardest part. Even more so, things like jQuery Mobile make it even harder. This is because jQuery Mobile rewrites the html that is initially loaded to be styled and work properly.
So, lets take a basic example and figure out how to style some ajax loaded content.
Lets look at a basic example of some jQuery Mobile page. Not thing fancy here, this just creates a basic page with a header and footer with some minor content.
2<html>
3 <head>
4 <title>Page Title</title>
5 <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.css" />
6 <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
7 <script type="text/javascript" src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.js"></script>
8</head>
9<body>
10
11<div data-role="page">
12
13 <div data-role="header" >
14 <h1>Mail Reader</h1>
15 </div><!-- /header -->
16
17 <div data-role="content">
18
19 <div class="ui-grid-a">
20 <div class="ui-block-a" style="width: 25%;'">
21 <ul data-role="listview" data-inset="true">
22 <li data-role="list-divider">My Email</li>
23 <li><a href="test.cfm" id="LoadInDiv" rel="external" class="linkDiv">Inbox</a></li>
24 <li><a href="test2.cfm" id="LoadInDiv2" rel="external" class="linkDiv">Outbox</a></li>
25 <li data-role="list-divider">Other</li>
26 <li><a href="index.html">Feeds</a></li>
27 </ul>
28 </div>
29 <div class="ui-block-b" style="width: 72%; padding-left: 20px;" id="contentDiv">
30
31 </div>
32 </div>
33 </div><!-- /content -->
34
35 <div data-role="footer">
36 <div data-role="navbar">
37 <ul>
38 <li><a href="#">Summary</a></li>
39 <li><a href="#" class="ui-btn-active">Favs</a></li>
40 <li><a href="#">Setup</a></li>
41 </ul>
42 </div><!-- /navbar -->
43 </div><!-- /footer -->
44</div><!-- /page -->
45</body>
46</html>
Rendered it looks like this:

So now lets take the base and put a little JavaScript in to it. This script will handle the click and load some external content.
2$(function(){
3 $('.linkDiv').click(function(event) {
4 $('#contentDiv').load($(this).attr('href'));
5 return false;
6 });
7});
8</script>
The return from the load call contains this:
2 <li><a href="index.html">This could be a subject</a> <span class="ui-li-count">12</span></li>
3 <li><a href="index.html">This could be a subject</a> <span class="ui-li-count">0</span></li>
4 <li><a href="index.html">This could be a subject</a> <span class="ui-li-count">0</span></li>
5 <li><a href="index.html">This could be a subject</a> <span class="ui-li-count">0</span></li>
6 <li><a href="index.html">This could be a subject</a> <span class="ui-li-count">0</span></li>
7 <li><a href="index.html">This could be a subject</a> <span class="ui-li-count">0</span></li>
8</ul>
When rendered I now get this:

Obviously this is wrong. The returned content has no styling or any "jQuery Mobile" magic in it. Even the number is off on the far right.
This happens because jQuery Mobile does not know to style the content. It is not that it can't, it just doesn't know it should. The content is pulled in after the styling has been applied. So, the question is, How can I get jQuery Mobile to apply its secret sauce to the loaded content?
Well, this was no simple task. I tried more things than I can count. Had many more failures than I would have liked to. At one point I got it to work fairly easily. However, subsequent loads would break the UI. I finally ran across a forum entry that I pieced together with other findings that gave me enough info to create a solution.
The first thing was not not use load. Load, while a great way to get content into something was not the right way to go here. What I I needed was to get the content and store it in a var. Then I had to "coherse" jQuery Mobile into doing it's magic.
So, a simple modification to the JS and I get the magic I am looking for.
2 $(function(){
3 $('.linkDiv').click(function(event) {
4 $.get($(this).attr('href'), function(data) {
5 $('#contentDiv').html(data).page();
6 $( "div[data-role=page]" ).page( "destroy" ).page();
7 });
8 return false;
9 });
10 });
11</script>
So what is the big change? Well, I replaced the load with get. This allowed me to store the result in a var. I could then take that return and put it into the div. Yes, load does that for me in one line. However, adding the ".page()" to the load chain had no effect. This way the page() worked. However, to be honest, I have no idea what the page() function does. I just know it has to be there. The next part of the magic sauce was the next line. Now just like the page function, I have no idea what this does either. All I know is that these two lines of code allow me to produce the result below.

Well, there you have it. A few magic beans and BOOM.
Till next time,
--Dave
#1 by Brad G. on 6/12/11 - 5:33 AM
$( "div[data-role=page]" ).page( "destroy" ).page();
will run page on all subpages. Instead, I only run it on the one just loaded:
$( "div#contentDiv[data-role=page]" ).page( "destroy" ).page();
#2 by Aaron Crowder on 7/8/11 - 10:11 PM
#3 by Larry Peery on 9/29/11 - 11:38 AM
#4 by oyuka on 11/29/11 - 1:54 PM
#5 by raven on 12/9/11 - 2:17 AM
#6 by VinÃcius E. on 12/9/11 - 1:10 PM
I did something a little different to load the content of file when the page loads but I'm not sure why it's not working. This is my JS code:
$(document).ready(function()
{
$.get('content.html', function(data)
{
$('#contentDiv').html(data).page();
$('div[data-role=page]').page('destroy').page();
});
}
Do you know what is wrong there? Thanks in advance.
#7 by Web Guy on 12/27/11 - 1:50 PM
Example:
Main Page
$('#ID_of_div_Im_loading_into').load(someURL);
bottom of Ajax called page
$('#ID_of_div_Im_loading_into').trigger("create");
This works because the create event initializes the jQm as if the page is a fresh load, but limits it to the content of the ID I specified. The load just pulled in the file as raw HTML content.
#8 by jose on 12/27/11 - 4:38 PM
If I just do
$("#search_results").html(data);
I will get the unformatted data.
#9 by jose on 12/27/11 - 5:19 PM
$("#search_results").html(data);
$('#search_results').listview('refresh');
#10 by Brian O on 12/28/11 - 9:09 PM
#11 by Sketchee on 2/4/12 - 8:26 PM
$('#div').trigger('create');
Just replace #div with whatever you want to update
#12 by Bobbytuck on 2/16/12 - 9:57 AM
<script language="JavaScript">
$(function(){
$('.linkDiv').click(function(event) {
$.get($(this).attr('href'), function(data) {
$('#contentDiv').html(data).trigger("pagecreate").trigger("refresh");
});
return false;
});
});
</script>
#13 by Pete on 3/7/12 - 1:32 PM
#14 by Rajkumar on 5/17/12 - 1:44 AM
Can anyone help me out.
Thanks in advance
$('.linkDiv').click(function(event) {
4 $.get($(this).attr('href'), function(data) {
5 $('#contentDiv').html(data).page();
6 $( "div[data-role=page]" ).page( "destroy" ).page();
7 });
8 return false;
9 });
#15 by Mike on 6/5/12 - 7:29 AM
$.get('urltoload', function (data) {
$('#divOfContentsToReplace').html(data).trigger("create");
});
#16 by Deric on 7/26/12 - 8:13 AM
#17 by Tomasso on 8/7/12 - 4:55 AM
$("#results > div").append("my collapsible content").trigger("create").trigger("refresh");
$( "div[data-role=page]" ).page( "destroy" ).page(); // that works for each page
Even with those two lines the jquery doesnt interpret the inserted DOM as a collapsible content, but only shows it.
#18 by shruti on 10/7/12 - 9:01 PM
#19 by Arvind Ravulavaru on 10/13/12 - 10:36 AM
#20 by dlatch on 10/21/12 - 2:28 AM
First the server side PHP to build the table.
When building the table, I set a javascript function:
<tr onClick="selectCust('<?=$row_cust_select['cust_id']?>');">
Then in my page code I have the following JavaScript:
function selectCust(cust) {
$.mobile.changePage( "#two", { transition: "slideup"} );
$.get('ajax/test.php?cust='+cust, function (data) {
$('#ajaxarea').html(data).trigger("create");
});
It does the trick!
}
#21 by dlatch on 10/21/12 - 2:30 AM
#22 by Jaso on 10/31/12 - 10:12 AM
#23 by Rafael Negherbon on 11/5/12 - 12:46 AM
my code...
$.get("backend/consultas/banner/teste.php", function (data) {
$('div[data-role=content]').html(data).page();
$( "div[data-role=page]" ).page( "destroy" ).page();
});
#24 by Pablo juarez on 12/8/12 - 4:22 PM
just what i need it,
Regards
#25 by John on 2/5/13 - 8:16 AM
#26 by kishore on 2/6/13 - 9:18 PM
#27 by kSaMi on 2/25/13 - 2:06 PM