Introduction
This article can also be found HERE as part of my blog.
How jQuery Mobile handles page changes
To understand this situation, you need to understand how jQuery Mobile works. It uses ajax to load other pages.
The first page loads normally. His HEAD and BODY are loaded into the DOM , and they are waiting for other content. When the second page loads, the BODY content is loaded into the DOM . More precisely, even BODY not fully loaded. Only the first div with the data-role = "page" attribute will be loaded, everything else will be discarded. Even if you have more pages inside BODY , only the first will be loaded. This rule applies only to subsequent pages, if you have more pages in the initial HTML , they will all be loaded.
This is why your button displays successfully, but the click event does not work. The same click event whose parent HEAD not taken into account during the page transition.
Here is the official documentation: http://jquerymobile.com/demos/1.2.0/docs/pages/page-links.html
Unfortunately, you will not find this in your documentation. The ether thinks this is general knowledge, or they forgot to describe it, like my other topics. (The jQuery Mobile documentation is large, but many things are missing.)
Solution 1
On the second page and on every other page, move the SCRIPT tag to BODY , for example:
<body> <div data-role="page"> // And rest of your HTML content <script> </script> </div> </body>
This is a quick fix, but still ugly.
A working example can be found in my other answer here: Pages do not start after change
Another working example: Page loaded differently with jQuery-mobile transition
Decision 2
Move all your javascript to the original first HTML. Collect everything and put it in one js file in HEAD . Initialize it after loading jQuery Mobile.
<head> <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> <script src="index.js"></script> // Put your code into a new file </head>
In the end, I will explain why this is part of a good solution.
Decision 3
Use rel = "external" in your buttons and each element that you use to change the page. Because of this, ajax will not be used to load the page, and your jQuery Mobile application will behave like a regular web application. Unfortunately, this is not a good solution in your case. Telephony should never work like a regular web application.
<a href="#second" class="ui-btn-right" rel="external">Next</a>
White papers, find the chapter: Communication without Ajax
Realistic solution
A realistic solution will use Solution 2 . But unlike solution 2, I would use the same index.js file and initialize it inside the HEAD all possible other pages.
Now you can ask me WHY ?
Phonegap , like jQuery Mobile, is buggy, and sooner or later an error will occur, and your application will fail (including the loaded DOM), if your every js content is inside the same HTML file, the DOM can be deleted, and Phonegap refresh your current page. If there is no javascript on this page, this will not work until the restart.
Final words
This problem can be easily fixed with good page architecture. If anyone is interested, I wrote an ARTICLE about the good jQuery Mobile page architecture. In a nut shell, I discuss that knowing how jQuery Mobile works is the most important thing you need to know before you can successfully create your first application.