Table of Contents for
The Modern Web

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition The Modern Web by Peter Gasston Published by No Starch Press, 2013
  1. The Modern Web
  2. Cover
  3. The Modern Web
  4. Advance Praise for
  5. Praise for Peter Gasston’s
  6. Dedication
  7. About the Author
  8. About the Technical Reviewer
  9. Acknowledgments
  10. Introduction
  11. The Device Landscape
  12. The Multi-screen World
  13. Context: What We Don’t Know
  14. What You’ll Learn
  15. A. Further Reading
  16. 1. The Web Platform
  17. A Quick Note About Terminology
  18. Who You Are and What You Need to Know
  19. Getting Our Terms Straight
  20. The Real HTML5
  21. CSS3 and Beyond
  22. Browser Support
  23. Test and Test and Test Some More
  24. Summary
  25. B. Further Reading
  26. 2. Structure and Semantics
  27. New Elements in HTML5
  28. WAI-ARIA
  29. The Importance of Semantic Markup
  30. Microformats
  31. RDFa
  32. Microdata
  33. Data Attributes
  34. Web Components: The Future of Markup?
  35. Summary
  36. C. Further Reading
  37. 3. Device-Responsive CSS
  38. Media Queries
  39. Media Queries in JavaScript
  40. Adaptive vs. Responsive Web Design
  41. Viewport-Relative Length Units
  42. Responsive Design and Replaced Objects
  43. Summary
  44. D. Further Reading
  45. 4. New Approaches to CSS Layouts
  46. Multi-columns
  47. Flexbox
  48. Grid Layout
  49. The Further Future
  50. Summary
  51. E. Further Reading
  52. 5. Modern JavaScript
  53. New in JavaScript
  54. JavaScript Libraries
  55. Polyfills and Shims
  56. Testing and Debugging
  57. Summary
  58. F. Further Reading
  59. 6. Device Apis
  60. Geolocation
  61. Orientation
  62. Fullscreen
  63. Vibration
  64. Battery Status
  65. Network Information
  66. Camera and Microphone
  67. Web Storage
  68. Drag and Drop
  69. Interacting with Files
  70. Mozilla’s Firefox OS and WebAPIs
  71. PhoneGap and Native Wrappers
  72. Summary
  73. G. Further Reading
  74. 7. Images and Graphics
  75. Comparing Vectors and Bitmaps
  76. Scalable Vector Graphics
  77. The canvas Element
  78. When to Choose SVG or Canvas
  79. Summary
  80. H. Further Reading
  81. 8. New Forms
  82. New Input Types
  83. New Attributes
  84. Datalists
  85. On-Screen Controls and Widgets
  86. Displaying Information to the User
  87. Client-side Form Validation
  88. The Constraint Validation API
  89. Forms and CSS
  90. Summary
  91. I. Further Reading
  92. 9. Multimedia
  93. The Media Elements
  94. Media Fragments
  95. The Media API
  96. Media Events
  97. Advanced Media Interaction
  98. Summary
  99. J. Further Reading
  100. 10. Web Apps
  101. Web Apps
  102. Hybrid Apps
  103. TV Apps
  104. Webinos
  105. Application Cache
  106. Summary
  107. K. Further Reading
  108. 11. The Future
  109. Web Components
  110. The Future of CSS
  111. Summary
  112. L. Further Reading
  113. M. Browser Support as of March 2013
  114. The Browsers in Question
  115. Enabling Experimental Features
  116. Chapter 1: The Web Platform
  117. Chapter 2: Structure and Semantics
  118. Chapter 3: Device-Responsive CSS
  119. Chapter 4: New Approaches to CSS Layouts
  120. Chapter 5: Modern JavaScript
  121. Chapter 6: Device APIs
  122. Chapter 7: Images and Graphics
  123. Chapter 8: New Forms
  124. Chapter 9: Multimedia
  125. Chapter 10: Web Apps
  126. Chapter 11: The Future
  127. N. Further Reading
  128. Introduction
  129. Chapter 1: The Web Platform
  130. Chapter 2: Structure and Semantics
  131. Chapter 3: Device-Responsive CSS
  132. Chapter 4: New Approaches to CSS Layouts
  133. Chapter 5: Modern JavaScript
  134. Chapter 6: Device APIs
  135. Chapter 7: Images and Graphics
  136. Chapter 8: New Forms
  137. Chapter 9: Multimedia
  138. Chapter 10: Web Apps
  139. Chapter 11: The Future
  140. Index
  141. About the Author
  142. Copyright

Hybrid Apps

If you want to publish your web apps through the big device app stores—such as the Apple App Store, Google Play, or the Windows Store—you need to create a hybrid app. These apps are similar to packaged apps in that all their resources are contained in a single archive, but they go one step further by adding a native shell, or wrapper, around the files, which ensures that the app can be integrated into the main operating system, providing security and better performance and also allowing access to restricted device APIs. A number of software solutions exist for making hybrid apps, but one of the most common—and certainly the easiest to learn—is PhoneGap.

PhoneGap

Although owned by Adobe, a commercial entity, PhoneGap is free, open source software that allows you to build semi-native mobile applications using web platform technologies. PhoneGap is a distribution of software called Apache Cordova, which used to be called PhoneGap before it was sold to Adobe and things became complicated. The names are often used interchangeably, but I’ll stick to calling it PhoneGap for consistency’s sake.

PhoneGap works across multiple platforms, chief among them iOS, Android, and Windows Phone, and one of its major selling points is that, as it’s a native OS wrapper around web platform code, it allows access to APIs on a device that are not always available through the browser. For this, it uses its own API, which matches standard APIs from each device where present, acts as a bridge when devices have differing implementations of an API, and otherwise creates new methods and objects where necessary.

To set up PhoneGap, you need to download the SDK for each device you want to target and, in some cases (notably for iOS), also get a developer certificate. When you’ve done all this, you complete the setup for each environment (full instructions are on the PhoneGap website, linked to in Appendix K) and start a new project, which creates a folder structure with a few key files, including the ones necessary for access to the API.

Granting Permissions

Once your setup is complete, you can add all the files to the project that your app needs to run, and then begin to take advantage of the PhoneGap API. Some of the properties in the API require that you add permission requests to a manifest file because (as I mentioned earlier) PhoneGap uses the XML-based widgets specification for requests. For example, to request access to the Notification API, you include the following XML element in the file config.xml:

<plugin name="Notification" value="org.apache.cordova.Notification"/>

Some platforms require no extra permissions, or make some methods available without requiring permissions, whereas others insist on permission requests for all methods. The API documentation has full details.

The PhoneGap API

The PhoneGap API is largely composed of a series of properties and methods on the navigator object. Some of these properties are already available through browsers. When they are, as with the geolocation object you learned about in Chapter 6, PhoneGap’s API chooses the native implementation first, falling back to its own implementation on systems where the property is not available.

Other properties require heightened permissions and, as such, are available on some platforms exclusively through PhoneGap. For example, consider the contacts object, which provides access to the user’s contact list—not something you want exposed to the Web without special authorization.

The contacts object has two functions: create() and find(). To select items from the contacts list, you use the latter with two required arguments: contactFields (a list of fields to return) and contactSuccess (a function to be run when a successful query takes place).

In the following code, I set up a query to get the display name and birthday of all contacts in the address book; then if this query is successful, it runs an anonymous function. This function loops through all of the results (in the contacts object I defined) and adds them to a string called contactDOB, which I use here to populate a list.

navigator.contacts.find(['displayName','birthday'], function (results) {
  var i, contactDOB;
  for (i=0; i < results.length; i++) {
    contactDOB += '<li>' + results[i].displayName + '(' + results[i].
birthday + ')</li>';
  }
});

Future browsers and web-based operating systems (such as Firefox OS) will probably provide direct access to some of these methods in the future, meaning the long-term future of PhoneGap will likely be in polyfilling features on legacy devices. PhoneGap’s developers are cognizant of this, as stated clearly in a blog post called “Beliefs, Goals and Philosophy”:

The ultimate purpose of PhoneGap is to cease to exist.

PhoneGap Events

In addition to its device properties API, PhoneGap also has a number of extremely useful events that register changes to the device’s status, from its network status and capability to the battery level. These events are critical if you want your app to provide users with the best possible experience, such as ensuring data is saved before a battery runs out or when network connection is lost.

The most important event is deviceready, which fires when PhoneGap has fully loaded and is ready to execute. The deviceready event is functionally identical to the DOMContentLoaded event you saw in Chapter 5 in that it also should be used in every script to ensure that all required libraries have been loaded and are in place before the rest of the scripts are run. As such, all of your functions that require access to the PhoneGap API should be run in the callback function:

document.addEventListener('deviceready', function () {
  // All PhoneGap-related functions
}, false);

Other events include pause and resume. The former is fired when the current application is closed and moved to a background process, and the latter when it becomes active again. For example, you may want to store data when the application is moved to a background process to make sure users doesn’t lose their work:

document.addEventListener('pause', function () {
  // Backup data
}, false);

Some events fire when an application disconnects or reconnects from a network (offline and online); when the volume or call buttons are pressed (volumedownbutton, volumeupbutton, startcallbutton, and endcallbutton); and when the battery status changes (batterystatus detects changes in status, whereas batterylow and batterycritical fire when the battery is low or in a critical state, as defined by the device).

Titanium

Another popular approach to publishing hybrid mobile apps is Appcelerator Titanium. Rather than acting as a wrapper around a web app like PhoneGap, Titanium is an SDK that lets you develop applications with JavaScript using native UI elements from the target device platform. This approach has the advantage of making your app look and feel more like a native app, but it requires that you write it in a way that isn’t compatible with the standard open web platform approach.

For that reason, I won’t cover Titanium further in this book, although knowledge of its existence is useful.