I have a page with some nabars, and when the "My Activity" navbar is clicked I want it to do something on load, and currently for testing purposes, I am just trying to alert a message to screen on load, but since it is a js file being called I am not sure, what Jquery mobile load function to use?
here is my html for the page:
<html>
<head>
<title>My Activity</title>
<meta name="viewport" charset="UTF-8" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css"/>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<script type="text/javascript" src="http://www.modernizr.com/downloads/modernizr-latest.js"></script>
<style>
</style>
</head>
<body>
<div data-role="page">
<div data-role="header" data-id="pagetabs" data-theme="a" data-position="fixed">
<div data-role="navbar">
<ul>
<li><a href="fb_feed.html" data-prefetch="true" rel="external" data-icon="custom">Facebook</a></li>
<li><a href="youtube_feed.html" data-prefetch="true" rel="external" data-icon="custom">YouTube</a></li>
<li><a href="my_activity.html" id="my_activitypage" data-prefetch="true" data-icon="custom" class="ui-btn-active ui-state-persist">My Activity</a></li>
</ul>
</div>
</div>
</div>
<div data-role="footer" data-theme="a" data-position="fixed"><h5>Social Stream</h5></div>
</div>
<script src="my_activity.js"></script>
</body>
</html>
and in my my_activity.js file I have tried different, page load functions. I tried the following, that only gets fired when you manually refresh the browser, doesn't get fired when you click on the navbar tab
$(document).on('pageinit', '[data-role="page"]', function(){
alert("hello");
});
Put this line:
Inside a page div, like this:
You are suffering from a classic jQuery Mobile problem. This topic is not discussed as a part of an official documentation so I will try to explain it to you.
When jQuery Mobile loads additional HTML pages it only loads page div (
data-role="page"
=, everything else is stripped. This is because HEAD already exist inside a DOM and another HEAD is not needs, same goes for a rest of the page.If you want to find more about it take a look at my blog ARTICLE that discuss this topic. There you will find a better description plus few solutions with examples. Read it carefully.