To understand this problem, you need to understand how jQuery Mobile works.
Your first problem is where you are trying to initialize JavaScript. From your previous answers, I see that you are using multiple HTML / ASP pages, and all of your javascript is initialized on the <head> page. This is the main problem. Only the first HTML file must contain JavaScript placed in the <head> content. When jQuery Mobile loads other pages in the DOM, it only loads the <div> with the data-role="page" attribute. Everything else, including <head> , will be discarded.
This is because the currently loaded page already has <head> . It makes no sense to load other <head> pages. This is even more. If you have several pages in the second HTML file, only the first will be loaded.
I will not try to invent warm water here, so there are links to my other answers that discuss this issue. There you can find several solutions:
There is more than enough information to give you an idea of ββwhat to do.
The main solutions to this problem are:
- Put all your JavaScript in the first HTML / ASP file
- Move JavaScript to
<body> ; more precisely, move it to the <div> using data-role="page" . As I pointed out, this is the only part of the page that will be loaded. - Use
rel="external" when switching between pages, because this will cause a full page to refresh. Basically, you are jQuery mobile that the page will act like a regular web application.
As Archer pointed out, you should use page events to initialize your code. But let me tell you more about this issue. Unlike regular regular web pages, when working with jQuery Mobile, the finished document usually starts before the page is fully loaded / reinforced inside the DOM.
This is why page events were created. There are several of them, but if you want your code to be executed only once (for example, if the document is ready), you should use the pageinit event. In any other case, use pagebeforeshow or pageshow .
If you want to learn more about page events and why they should be used instead of a finished document, look at this article in my personal blog. Or find here .
Gajotres
source share