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

On-Screen Controls and Widgets

The new elements you’ve seen so far are all based on a simple text box, but some form elements also provide richer on-screen controls; in HTML 4.01, for instance, think of select and checkbox. But as I mentioned in the introduction, many other widget types are commonly used by developers and designers—think of date pickers and number range sliders—so HTML5 has adopted and standardized these patterns.

How these controls and widgets appear depends on the browser and platform in which they’ve been implemented; the HTML5 specification notes only that these controls could be used and isn’t prescriptive as to how they appear. If a browser doesn’t support the controls natively, they should fall back to look like a standard text input.

Numbers

The new HTML5 input types discussed so far already cover various text formats. But, of course, many forms require that the user enter a number, for instance, credit card details, an area code, or a quantity.

The number input is the field for inputting numbers. It is often displayed as a text field, but some browsers also add controls—often a pair of arrows, one up, one down—for incrementing or decrementing the value.

Similar to number is the range input, which lets users enter a value without requiring them to be too precise about the exact figure; to allow this, many browsers style this element as a slider.

<input type="number">
<input type="range">

You can compare number and range as they’re displayed in Chrome for Android in Figure 8-4.

The number and range types as displayed in Chrome for Android
Figure 8-4. The number and range types as displayed in Chrome for Android

Both of these types have some new attributes in common: max and min are number values that set the maximum and minimum (did you work that out for yourself?) permitted values, and step is the number by which the value is incremented or decremented. The following code shows how all three could work; the number input has an initial value of 50 and can be incremented or decremented by 10 at a time to reach a minimum of 10 or a maximum of 100:

<input type="number" max="100" min="10" step="10" value="50">

You can also manipulate these values with JavaScript using the stepUp() and stepDown() methods. Each takes a single integer value, which moves the value of the input by the specified number of steps; for example, this syntax reduces the range input value by 3 steps:

var foo = document.querySelector('input[type=range]');
foo.stepDown(3);

Each method returns an error if the specified number of steps causes the value to exceed the element’s max or min values.

When working with numeric fields, you may want to take advantage of a new DOM property defined in HTML5, valueAsNumber. This property is similar to the existing value property but returns the value as a number rather than a string, meaning you don’t need to convert between types using parseInt(). Using valueAsNumber is simplicity itself:

var foo = document.querySelector('input[type=number]');
bar = foo.valueAsNumber;

Dates

Another popular data pattern for forms is a date or time field, used in situations such as when asking the user to enter a date of birth or choose a delivery time and date. Often these are rendered using a JavaScript-created date picker, a common widget aimed at helping users choose a date from a range shown on screen so they don’t have to worry about conforming to your chosen date pattern.

HTML5 has a range of new input types for date and form fields, and many browsers have added native date-picker widgets to enhance them. Probably the most commonly implemented is date, which lets the user select a single date from the widget:

<input type="date">

The implementation method varies across browsers, with mobile and tablet devices varying quite significantly from desktop and laptop browsers. You can see some examples of this variety in Figure 8-5.

The native date-picker widget on the Chrome for Android tablet (left), the iPhone (center), and the Chrome desktop (right)
Figure 8-5. The native date-picker widget on the Chrome for Android tablet (left), the iPhone (center), and the Chrome desktop (right)

HTML5 also has a series of other date and time inputs: If you need to be more general about dates, you can use month to select an entire month and week for an entire week; or if you require a time without any associated date, you can use time to choose hours and minutes. A couple examples of different time and date pickers are shown in Figure 8-6.

A time picker on Chrome for Android (left) and a month picker for iPhone (right)
Figure 8-6. A time picker on Chrome for Android (left) and a month picker for iPhone (right)

If you require a date and a time, the datetime input requests both. This field requires a value in the format YYYY-MM-DDTHH:MMZ, where the Z is a shorthand code for the UTC time zone. For example, to submit a time of 2 PM on April 1st, 2014, you would use 2014-04-01T14:00Z. If the time zone isn’t required, you could use datetime-local. For both types, the picker widget would have fields for both date and time, as shown in Figure 8-7.

All the date-related input types are demoed in input-types-dates.html; open the page in different browsers and see how they’re displayed.

As with the number input types, the max and min attributes are permitted, but they must use a valid date or time format; a full datetime would require YYYY-MM-DDTHH:MMZ, whereas the month would require only YYYY-MM. The step attribute is also allowed, but its time period depends on the element used: a day, a week, a month, or a time in seconds.

A datetime picker on Chrome for Android
Figure 8-7. A datetime picker on Chrome for Android

So putting that all together, you could use attributes somewhat like the following, where the month input would allow the user to select only dates between January 2012 and June 2016; the step attribute would be in play only if the stepDown() and stepUp() methods were used:

<input type="month" max="2016-06" min="2012-01" step="3">

Warning

If you have strict limits on required dates, don’t rely on the max and min attributes, as they’re not supported by some user agents; always use JavaScript and server-side validation to ensure dates are in range.

As numbers have the valueAsNumber DOM property, so dates have valueAsDate. This property works in the same way, but returns a date-formatted value; for example, given the date 04/01/2014, the value property would return 2014-04-01, whereas the valueAsDate property would give Tue Apr 01 2014 01:00:00 GMT+0100 (BST) (in my time zone, at least).

var foo = document.querySelector('input[type=date]');
bar = foo.valueAsDate;

Color

If you’re building an app that allows the user some level of customization, you may be interested to know that HTML5 has a color input, which will, if implemented, show either the system default color picker or a proprietary widget, depending on the browser:

<input type="color">

Try it for yourself in input-types-more.html. Figure 8-8 shows how Chrome implements it for Ubuntu.

Chrome uses the native color picker of your OS for the color element; here’s how it looks in Ubuntu.
Figure 8-8. Chrome uses the native color picker of your OS for the color element; here’s how it looks in Ubuntu.