jQuery Mobile: Data-role

This entry is part 6 of 73 in the series jQuery Mobile

The jQuery Mobile framework uses HTML5 data- attributes to allow for initialization and configuration of widgets.

Example widgets are:

  1. button,
  2. page,
  3. header and
  4. footer etc.

In the previous example, we already have some of these attributes for the data-role.

<div data-role='page'>
    <div data-role='header'><h1>My Mobile App</h1></div>
    <div data-role='content'>
        <p>Look at the button!</p>
        <a href='#' data-role='button'>I am a button</a>
    </div>
</div>
The attribute definitions
  • The data-role=’page’ is the page displayed in the browser
  • The data-role=’header’ creates a toolbar at the top of the page
  • The data-role=’content’ where the main content of the page is located
  • The data-role=’button’ defines a button

The full data attributes are defined in this page.

jQuery Mobile: Link to a page

This entry is part 7 of 73 in the series jQuery Mobile

Say you have a website yahoo.com and you would like your app to only bring your users to yahoo.com.

Code
<!DOCTYPE html>
<html>
<head>
    <link href='http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css' rel='stylesheet'/>
    <script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
    <script src='http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js'></script>
    <title>Upwhere</title>
</head>
<body>
<div data-role='page'>
    <div data-role='header'><h1>My Mobile App</h1></div>
    <div data-role='content'>
        <p>Bring Me To Yahoo!</p>
        <a href='http://yahoo.com' data-role='button'>Click Me</a>
    </div>
</div>
</body>
</html>

This is done by modifying the href in the data-role=’button’ line.

<a href='http://yahoo.com' data-role='button'>Click Me</a>

Clicking on the button will bring you to yahoo.com.  You can try the code here.

jQuery Moble: Page movement

This entry is part 8 of 73 in the series jQuery Mobile

In this post, we first learn how to define a second page.  This is done by:

<div data-role='page' id='pagetwo'>
    <div data-role='header'><h1>My Mobile App</h1></div>
    <div data-role='content'>
        <p>You are at page two!</p>
        <a href='#pageone' data-role='button'>Go Back</a>
    </div>
</div>

We create another div with data-role=’page’.  We also need to put an id to this page.

Since this app will only keep cycling between pageone and pagetwo, we have a href pointing back to pageone in line 21 below.

There are 2 data-roles with page attributes in lines 10 and17.

The whole code structure is as below:
<!DOCTYPE html>
<html>
<head>
    <link href='http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css' rel='stylesheet'/>
    <script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
    <script src='http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js'></script>
    <title>Codecrawl</title>
</head>
<body>
<div data-role='page' id='pageone'>
    <div data-role='header'><h1>My Mobile App</h1></div>
    <div data-role='content'>
        <p>Your page one content here</p>
        <a href='#pagetwo' data-role='button'>Click Me</a>
    </div>
</div>
<div data-role='page' id='pagetwo'>
    <div data-role='header'><h1>My Mobile App</h1></div>
    <div data-role='content'>
        <p>You are at page two!</p>
        <a href='#pageone' data-role='button'>Go Back</a>
    </div>
</div>
</body>
</html>
Try The Code

You can try running the script here.

jQuery Mobile: Fixed footer

This entry is part 9 of 73 in the series jQuery Mobile

Using our earlier example, we put a fixed footer on both of the pages.

Fixed footer can be added with the following markup:

<div data-role="footer" data-position="fixed">
	<h1>Fixed Footer!</h1>
</div>

On page one, the code will be as below:

<div data-role='page' id='pageone'>
    <div data-role='header'><h1>My Mobile App</h1></div>
    <div data-role='content'>
        <p>Your page one content here</p>
        <a href='#pagetwo' data-role='button'>Click Me</a>
    </div>
    <div data-role='footer' data-position="fixed">
	<h1>Fixed Footer!</h1>
</div>
Try The Code Fixed Footer Code Block
<!DOCTYPE html>
<html>
<head>
    <link href='http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css' rel='stylesheet'/>
    <script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
    <script src='http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js'></script>
    <title>Upwhere</title>
</head>
<body>
<div data-role='page' id='pageone'>
    <div data-role='header'><h1>My Mobile App</h1></div>
    <div data-role='content'>
        <p>Your page one content here</p>
        <a href='#pagetwo' data-role='button'>Click Me</a>
    </div>
    <div data-role='footer' data-position='fixed'>
	<h1>Fixed Footer!</h1>
</div>
</div>
<div data-role='page' id='pagetwo'>
    <div data-role='header'><h1>My Mobile App</h1></div>
    <div data-role='content'>
        <p>You are at page two!</p>
        <a href='#pageone' data-role='button'>Go Back</a>
    </div>
    <div data-role='footer' data-position="fixed">
	<h1>Fixed Footer!</h1>
</div>
</div>

</body>
</html>

 

 

jQuery Mobile: Dialog box

This entry is part 10 of 73 in the series jQuery Mobile

Instead of going to a page, we can also open pagetwo as a dialog box.

To do that, we add data-dialog=’true’.

<div data-role='page' data-dialog='true' id='pagetwo'>

The only change is in putting data-dialog=’true’ in pagetwo div.

Dialog Box
<!DOCTYPE html>
<html>
<head>
    <link href='http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css' rel='stylesheet'/>
    <script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
    <script src='http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js'></script>
    <title>Upwhere</title>
</head>
<body>
<div data-role='page' id='pageone'>
    <div data-role='header'><h1>My Mobile App</h1></div>
    <div data-role='content'>
        <p>Your page one content here</p>
      <a href='#pagetwo' data-role='button'>Click Me</a>
    </div>
    <div data-role="footer" data-position="fixed">
	<h1>Fixed Footer!</h1>
</div>
</div>
<div data-role='page' data-dialog='true' id='pagetwo'>
    <div data-role='header'><h1>My Mobile App</h1></div>
    <div data-role='content'>
        <p>You are at page two!</p>
        <a href='#pageone' data-role='button'>Go Back</a>
    </div>
</div>

</body>
</html>
Try The Code