3

I use jQuery Mobile. But when I load page in another html file as a page, the javascript in that head tag won't execute. I checked the source and found that the head tag is still the original one's. So what should I do to run script when page loaded?

EDIT1:

I also tried to write js code inside body tag, but the script won't stop when leave away. I have a timer so that there'll be two timer racing.

EDIT2:

I've uploaded pages:

main.html http://pastebin.com/0Y9Xfrtw

recharge.html http://pastebin.com/j9HfUvqZ

recharge.js http://pastebin.com/K5eCwMAb

liuyanghejerry
  • 3,771
  • 5
  • 32
  • 38
  • jQuery Mobile ? hmm... - in what browser? Anyway, try reading into this http://stackoverflow.com/questions/2384204/is-there-a-difference-between-ready-and-document-ready – c69 Nov 03 '11 at 11:33
  • @c69 ready() function wasn't recommended in jQuery Mobile, but the function pageInit() instead, though doesn't work well for me... – liuyanghejerry Nov 03 '11 at 11:43
  • is it giving any errors ? or just 'nothing happens' ? – c69 Nov 03 '11 at 11:45
  • @c96 If put them in head tag, it won't work, because the head tag won't be loaded, while put them in body tag, it won't stop work so when pages switch, script will race. – liuyanghejerry Nov 03 '11 at 11:52
  • @c69 I've uploaded pages to pastebin. See my last EDIT. – liuyanghejerry Nov 03 '11 at 11:56
  • @Naning I tried, but will also run multiple times when loading. I checked http://jquerymobile.com/test/docs/pages/page-navmodel.html and it doesn't give a good solution. – liuyanghejerry Nov 03 '11 at 12:20

1 Answers1

4

UPDATE #2:

Try stopping the timer before the page transition. Since you are using multiple pages I'm not sure the timer is stopping when navigating elsewhere. Try testing the different Page Transition Events:

In the example above I used:

pagebeforehide

UPDATE:

I've tried to ad your code from PasteBin into JSFiddle

This is working I think, as I see the timer count down on the recharge page and if it hits zero navigate back to the home page:

I get an error on this method:

Uncaught TypeError: Object [object Object] has no method 'everyTime'

Alternative to everyTime

with jQM all JavaScript (for each page) should be in the page that's loaded first. so if you load index.html place all JavaScript in this page. To execute the JavaScript for a specific page you can use one of the Page initialization events, Docs:

JS

$('#page_id').live( 'pageshow',function(event, ui){
    alert( 'This page was just hidden: '+ ui.prevPage);
});

Example:

JS

$('#home').live( 'pageshow',function(event, ui){
    alert( 'We are back home');
});

$('#page2').live( 'pageshow',function(event, ui){
    alert( 'This will only execute on Page 2');
});

HTML

<div data-role="page" id="home">
    <div data-role="content">

        <ul data-role="listview" data-inset="true" data-theme="c" data-dividertheme="f">
            <li data-role="list-divider">Overview</li>
            <li><a href="#page2">Page 2</a></li>
        </ul>

    </div>
</div>

<div data-role="page" id="page2">
    <div data-role="content">

        <ul data-role="listview" data-inset="true" data-theme="c" data-dividertheme="f">
            <li data-role="list-divider">Overview</li>
            <li><a href="#home">Go Back Home</a></li>
        </ul>

    </div>
</div>
Community
  • 1
  • 1
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
  • What if page2 is in a independent file? – liuyanghejerry Nov 03 '11 at 13:45
  • shouldn't matter, still all JS needs to go in the main page loaded – Phill Pafford Nov 03 '11 at 13:45
  • @liuyanghejerry http://jsfiddle.net/QgSaw/4/ should be working with the timer functionality – Phill Pafford Nov 03 '11 at 13:55
  • I don't know why but I found that the timer is racing. When I go to another page(by the side navi) and go back to the page, there'll be two timer racing...Check it out:http://qtplus.info/contest/main.html – liuyanghejerry Nov 03 '11 at 14:20
  • hmm not sure your live eevnt with pagehide is working, try $("#recharge").live('pagebeforehide', function() { var past = 0; $('#recharge').stopTime(); $('#timer span > .ui-btn-text').html('60秒后自动退出'); }); – Phill Pafford Nov 03 '11 at 15:02