1

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");
});
Dot
  • 279
  • 1
  • 9
  • 21
  • Even this doesn't work. $(document).on( "click", '#my_activity.html', function( event ){ alert('hello'); }): – Dot Jul 19 '13 at 15:06

1 Answers1

2

Put this line:

<script src="my_activity.js"></script>

Inside a page div, like this:

<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 data-role="footer" data-theme="a" data-position="fixed"><h5>Social Stream</h5></div> 
    <script src="my_activity.js"></script>    
</div>

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.

Gajotres
  • 57,309
  • 16
  • 102
  • 130
  • ok i put it inside a page div, but would I use to load the my_activity script? $(document).on('pageinit', '[data-role="page"]', function(){ alert("hello"); }); or would I use a click on load? – Dot Jul 19 '13 at 15:43
  • You should bind click event inside a pageinit event. This is because button may or may not be available / styled when you try to do only click binding. Doing it inside a pageinit will give you an insurance button is really there and active. – Gajotres Jul 19 '13 at 15:48
  • I don't understand what you mean. I want to be able to do certain functions only when the tab, "my activity" is clicked. doing this in the my_activity.js file fires the events for all tabs; $(document).on('pageinit', '[data-role="page"]', function(){ alert("testing page load"); $(document).on( "click", function( event ){ alert('hello'); }); }); instead of "document" what would be my identifier? – Dot Jul 19 '13 at 15:56
  • yh but i want the alert to happen only for the "my activity tab" not all tabs like the jsfiddle example does. – Dot Jul 19 '13 at 16:08
  • Are you talking about whole navbar inside a specific page or just one navbar tab? – Gajotres Jul 19 '13 at 16:30
  • @gajortres, but my href= "my_activity.html" so eventhough I include id my_activitity page, it doesn't filre the load. :( – Dot Jul 19 '13 at 16:49
  • Tell me can you mail me your project and I will fix it? – Gajotres Jul 19 '13 at 17:01
  • dragan.gaic@gmail.com – Gajotres Jul 19 '13 at 17:06
  • Mail sent have you received it? – Dot Jul 19 '13 at 17:53