Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery mobile page injection

I have this simple html (I am hosting my own files and it works)

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"  href="JQM/jquery.mobile-1.0.1.min.css" />
<script src="JQM/jquery.js"></script>
<script src="JQM/jquery.mobile-1.0.1.min.js"></script>
<script src="pages.js"></script>
</head> 
<body onload="main_page();"> 

<div data-role="page">
<div data-role="header">
    <h1>My Title</h1>
</div>
<div id="main_div" data-role="content"> 
</div>
</div>

</body>
</html>

on the onload I have to call this function and add the content generated in js to id="main_div" or data-role="content"

function main_page(){//trips menu

 var this_page =' ';
 var this_body='';

 this_body += '<ul data-role="listview" data-inset="true" data-filter="true">';
 this_body += "<li><a href=\"#\">Check my Inbox</a></li>";
 this_body += "<li><a href=\"#\">Tutors/ Post Tutors</a></li>";
 this_body += "<li><a href=\"#\">Books / Post Books</a></li>";
 this_body += "<li><a href=\"#\">Invite friends</a></li>";
 this_body += "<li><a href=\"#\">Forum</a></li>";
 this_body += '</ul>';
 this_page += this_body;

 var $page = $( pageSelector ),
        $content = $page.children( ":jqmData(role=content)" );
    $page+=this_page;

 $.mobile.changePage(#,$page);
}

this is just a simple example of what I have to do. But no matter what I do it doesn't work....

like image 569
user1342645 Avatar asked Feb 24 '26 18:02

user1342645


1 Answers

You can't load a page like that but If your goal is to load this menu you can achieve that with this snippet :

$(document).on('pageinit',function(){
    // this is the mobile onload event
 var this_page =' ';
 var this_body='';

 this_body += '<ul id="menu" data-role="listview" data-inset="true" data-filter="true">';
 this_body += "<li><a href=\"#\">Check my Inbox</a></li>";
 this_body += "<li><a href=\"#\">Tutors/ Post Tutors</a></li>";
 this_body += "<li><a href=\"#\">Books / Post Books</a></li>";
 this_body += "<li><a href=\"#\">Invite friends</a></li>";
 this_body += "<li><a href=\"#\">Forum</a></li>";
 this_body += '</ul>';
 this_page += this_body;

 $('#main_div').append($(this_page));
 //EDIT : you also need to trigger the listview creation after inserting, almost forgot this trick :P
 $('#menu').listview();

});

you can remove the onload function from your body with that method. Is that what you want to do?

like image 74
TecHunter Avatar answered Feb 27 '26 08:02

TecHunter