In my previous post, I had visited the behavior of the jQuery Mobile library data-title attribute under different scenarios when using a multi-page template. In this post, I will outline the behavior when using a single-page template.
This is a working example. Play with it to see how the page transitions occur. Now lets go through the similar scenarios as in previous article:
Consider the below single-page template code. Click here to launch the below code in a separate browser.Launch
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
<div id="page1" data-role="page" data-title="Mainpage data-title">
<h1>Header of Main Page</h1>
<p><a href="secondpage-title.html" data-role="button">Go to Page 2</a></p>
<div id="page2" data-role="page" data-title="Page 2 data-title">
<h1>Header of Page 2</h1>
<p><a data-role="button" data-rel="back">Go Back</a></p>
- When you first load this html, main page is displayed, you will first see the title as "Main Page". The <title> tag (document.title) always gets precedence.
- If the <title> tag is not present, you will get the address of the page shown as the title.
- Now click on the "Go to Page 2" button, you will slide into page 2 and the title is shown as "Page 2 data-title", you will see this is a minimal html file. HTML5 makes semantics much more simpler. Browser knows what to do. Again here, since Ajax transition was used, the data-title is picked up and not the document.title which was "Second Page".
- From page 2, click on the "Go Back" button, now main page will open and the title is shown as "Main Page", itself and not the data-title text. This is different from multi-page template behavior. Now why did that happen? The reason here is, when Main page was first loaded, document.title gets populated, which gets precedence and is used. Now every visit to this page uses the same document.title value instead of what is there in data-title attribute. But since page 2 (newly loaded) was via Ajax, the data-title gets picked up first and not the <title> tag. This is the way it is implemented.
- Now in the code, remove the data-title attribute for page 2, navigate to page 2, you will see the title shown as "Second Page", now the <title> is picked up.
- If <title> was missing in page 2, "Header of Page 2" is displayed, the text from the header.
- Now in the code, remove the data-title attribute for main page, navigate to main page from page 2, you will see the title again being shown from the data-title tag this time. So the title is shown as "Mainpage data-title".
- Now in the code, remove the entire "header" div from page 2, and also remove the data-title from page 2, now if you navigate into page 2 from the main page, then the current title of main page is used.
- how come there is no html and head tag in page 2 source code ? If i refresh the page 2 it doesn't render correctly as above tags are missing.
Isn't it important to add <html and <head tags in every single page ?
- A question : If I have to render dynamic data that loads to DOM of 2nd page through the query parameter of the 2nd page url, how do I do that ? can u help me in that ?