Before we begin, its important to know
“What is jQuery Mobile”? Well, jQuery mobile is a framework created for making platform independent mobile applications. It is built using HTML5, CSS3, and the very popular jQuery library.
Lets begin…
jQuery mobile uses HTML5 extensively so to get full advantage of jQuery mobile, we need to use HTML 5. So the very first thing include in your HTML file is “HTML5 doctype”.
Next thing to tell the browser about width of your page. This is important for mobile application as if not specified that it may appear awkward on mobile devices. In the head tag of page, a meta viewport tag sets the screen width to the pixel width of the device
1 |
<meta name= "viewport" content= "width=device-width, initial-scale=1" > |
Now lets load the jQuery mobile framework in head section. We shall use the CDN enabled links for better performance. Please take a note we need to reference jQuery mobile CSS, jQuery and jQuery mobile js file.
That’s all for the head section of the page. Before we move to body of the page, it is important to understand the page structure of jQuery mobile app. As seen in below image, it has 3 sections “Header”, “Content” and “Footer”. The page, its header, footer, and content are all <div> containers, which are styled by using the HTML 5 data-role attributes.
In the body, we shall create different page blocks, where pages are nothing but <div> elements with HTML 5 data attributes.
Let’s first create a single page. As mentioned earlier in the post, jQuery mobile uses HTML 5. So to define a page, it uses HTML5′s data attribute “data-role=page”.
Remember, we can have many such <div> elements with “data-role=page” attribute within single <body> tag. A body tag can hold multiple mobile pages too.
Now, we shall create header, content and footer section of the page. This is again achieved using HTML 5 data-role attributes.
1 |
<div data-role= "header" > |
3 |
<div data-role= "content" > |
5 |
<div data-role= "footer" > |
Let’s go ahead and put some content in all 3 sections.
1 |
<div data-role= "header" ><h1>Welcome!!!</h1> |
4 |
<div data-role= "content" ><p>Welcome to my blog and you are reading how to write your first jQuery mobile app.</p> |
7 |
<div data-role= "footer" ><h1>jQuery By Example</h1> |
Putting is all together then this is how it looks.
04 |
<meta name= "viewport" content= "width=device-width, initial-scale=1" > |
05 |
<title>My First jQuery Mobile Application</title> |
13 |
<div data-role= "page" > |
15 |
<div data-role= "header" ><h1>Welcome!!!</h1></div> |
17 |
<div data-role= "content" > |
18 |
<p>Welcome to my blog and you are reading how to write your first jQuery mobile app.</p> |
21 |
<div data-role= "footer" ><h1>jQuery By Example</h1></div> |
So, now when you view this page in mobile it will look like this..
Congratulations, You have just done with your first jQuery Mobile application.
When I started with my first app, the one thing got my attention instantly. Question came to mind about the styles applied to the page, the colors used for header, footer and content containers. As while creating the app, I have not specified any styles. Well, welcome to jQuery mobile themes.
The framework provides 5 default basic color schemes called “color swatches“. They are named a, b, c, d and e. And by default, swatch a is used when you create a page. This provides combination of white and black colors, as seen in the previous screenshot.
Now, you must be thinking how should I change the theme? Well you can change the color swatch of your page and header/footer by using the data-theme attribute.
1 |
<div data-role= "header" data-theme= 'b' ></div> |
You can also define different themes for different containers.
1 |
<div data-role= "header" data-theme= 'b' > |
3 |
<div data-role= "content" data-theme= 'c' > |
5 |
<div data-role= "footer" data-theme= 'b' > |
Let’s change the theme to our demo and see how it looks.
Along with these 5 predefined themes, jQuery mobile also allows you to create custom theme using ThemeRoller.
Now, lets modify the demo and add a button in header section. For now, on click of this button we will redirect user to Home.html. Although the page doesn’t exists but for demo its fine.
Defining a button is quite simple. Buttons are coded with standard HTML anchor and input elements, then enhanced by jQuery Mobile to make them more attractive and useable on a mobile device. Use anchor links (a elements) to mark up navigation buttons, and input or button elements for form submission. Lets define a navigation button in header section.
1 |
<div data-role= "header" > |
2 |
<a href= "Home.html" >Home</a> |
You can also define icons for the button using predefined icon set
provided by jQuery Mobile. To define an icon, use “data-icon” attribute.
I hope you find this post useful and good enough to start with jQuery mobile. By now, you must have an idea about creating single page, changing themes and defining buttons. In next post, I will show more about creating multiple pages and navigation between pages.