Thursday, December 15, 2011

jQuery Mobile data-title behavior in single-page template

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.
Consider the below single-page template code. Click here to launch the below code in a separate browser.


<!DOCTYPE html>
    <title>Main Page</title>

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="" /> 
    <script src=""></script>
    <script src=""></script>
    <div id="page1" data-role="page" data-title="Mainpage data-title">
      <div data-role="header">
        <h1>Header of Main Page</h1>
      <div data-role="content">
        <p><a href="secondpage-title.html" data-role="button">Go to Page 2</a></p>


<title>Second Page</title>
<div id="page2" data-role="page" data-title="Page 2 data-title">
  <div data-role="header">
<h1>Header of Page 2</h1>
  <div data-role="content">
<p><a data-role="button" data-rel="back">Go Back</a></p>

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:

  1. 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.
  2. If the <title> tag is not present, you will get the address of the page shown as the title.
  3. 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".
  4. 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.
  5. 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.
  6. If <title> was missing in page 2, "Header of Page 2" is displayed, the text from the header.
  7. 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".
  8. 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.
So when you compare the behavior of page titles from single-page to multi-page templates, the order of precedence is different while picking the title of page 2 from the <title> tag (as outlined in point 4 above).

1 comment:

  1. Hey Chetan,

    - 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 ?