Book Image

jQuery Mobile Cookbook

By : Chetan Jain
Book Image

jQuery Mobile Cookbook

By: Chetan Jain

Overview of this book

jQuery Mobile is an award winning, HTML5/CSS3 based open source cross-platform UI framework. It offers a very cool and highly customizable UX. It is built on the popular jQuery library and uses declarative coding making it easy to use and learn. It is the market leader today considering the numerous browsers and platforms that it supports."jQuery Mobile Cookbook" presents over a hundred recipes written in a simple and easy manner. You can quickly learn and start writing code immediately. Advanced topics such as using scripts to manipulate, customize, and extend the framework are also covered. These tips address your common everyday problems. The book is very handy for both beginner and experienced jQuery Mobile developers.You start by developing simple apps using various controls and learn to customize them. Later you explore using advanced aspects like configurations, events, and methods.Develop single and multi-page applications. Use caching to boost performance. Use custom transitions, icon sprites, styles, and themes. Learn advanced features like configurations, events, and methods. Explore future trends by using HTML5 new features and semantics with jQuery Mobile."jQuery Mobile Cookbook" is an easy read and is packed with practical tips and screenshots.
Table of Contents (19 chapters)
jQuery Mobile Cookbook
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Using JS Bin to create a simple application


JS Bin is an open source web application built by Remy Sharp, available at http://www.jsbin.com. JS Bin allows you to directly enter your HTML, CSS, and JavaScript code online, and also allows you to include the required jQuery and jQuery Mobile libraries. You can add and directly run your JavaScript code and preview the output on your browser. You can also share your code and collaborate with others for review or troubleshooting. You can finally download your code once everything works as desired. It is a very popular tool used by many jQuery Mobile developers. This recipe shows you how to create a simple jQuery Mobile application using JS Bin.

Getting ready

The code in this recipe was created using the JS Bin web application available at http://www.jsbin.com. The code is available in the code/01/jsbin source folder. You can launch the code using the URL http://localhost:8080/01/jsbin/main.html.

How to do it...

  1. Launch the JS Bin web application tool at the URL http://www.jsbin.com, and you will see a basic HTML template.

  2. Select the Add Library link on the top-left panel, and include the latest jQuery Mobile library files. Next, edit the <head> section, as shown in the following code snippet:

    <html>
      <head>
        <link href="http://code.jquery.com/mobile/latest
          /jquery.mobile.css" rel="stylesheet" type="text/css" />
        <script src="http://code.jquery.com
          /jquery-1.7.1.min.js"></script>
        <script src="http://code.jquery.com
          /mobile/latest/jquery.mobile.js"></script>
        <meta name="viewport" content="width=device-width, 
          initial-scale=1">
        <title>Welcome using JS Bin</title>
      </head>
  3. Add code to the <body> section to create a simple jQuery Mobile page:

      <body>
        <!-- Main Page -->
        <div id="main" data-role="page">
          <div data-role="header">
            <h1>Welcome - JS BIN</h1>
          </div>
          <div id="content" data-role="content">
            <p>The jQuery Mobile Cookbook</p>
          </div>
          <div data-role="footer">
            <h4>Enjoy reading the book ...</h4>
          </div>
        </div>
      </body>
    </html>
  4. The preview or output is now visible in the Output pane on the right side of the screen.

  5. You can now download the source file (or copy-and-paste into a local file) to have a simple working jQuery Mobile application.

How it works...

Launch the JS Bin web application in your browser. You will see the following screen in your browser, with a basic HTML template (which you can edit) on the left side. A menu bar is available at the top and an Output pane is available on the right, to instantly preview the output of your code:

You can click on the various menu options and see how the CSS or JavaScript panes can be made visible or hidden. Selecting the Auto-run JS option will allow you to run your JS code automatically; you can leave it on. You can also manually run the script by clicking on the Run with JS button.

Click on the Add library menu option and select the jQuery Mobile Latest option as shown in the following screenshot:

This will include the links and references to the jQuery Mobile and jQuery libraries in the <head> section of the HTML.

Note

When you add the jQuery Mobile library to your code using JS Bin, make sure you edit and set the correct versions for both jQuery Mobile and jQuery libraries that you want to use with your application. When this recipe was written, jQuery v1.6.4 was being used in JS Bin, whereas jQuery v1.7.1 is recommended to be used with jQuery Mobile v1.1.1.

Next, edit the <meta> tag to set the correct viewport width and scale, as shown in the code. Then, add a page to the <body> tag using a div tag with data-role="page". Create the header (data-role="header"), page content (data-role="content"), and footer (data-role="footer"), as shown. As you add these sections, you will notice that the Output pane on the right side of the screen gets updated and shows the output preview of your code.

You can also add CSS styles and JavaScript, and check how it works. Finally, your code is ready and you can copy-and-paste it locally into your editor. You can also click on the JS Bin menu option at the top-left to download the file. Now, launch the local file in your browser, and you will see that the output matches what was displayed in the Output pane of JS Bin.

There's more...

This recipe shows you the simple steps required to create a basic jQuery Mobile application using JS Bin. JS Bin provides many features that are nice to use, such as creating and using ready templates, saving and forking your code using GitHub, and cloning your code. This tool is best suited for when you want to store your files online and collaborate on your source files. For more information and tutorials on using JS Bin, refer to http://jsbin.tumblr.com/.

Note

You can register for free and log in to JS Bin with your user account to make use of the save, download, or clone features. Only the basic features are available without user login.

See also

  • The Writing your first jQuery Mobile application recipe