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

Displaying Information to the User

As well as accepting input from users, sometimes displaying information back to users is helpful, perhaps to show their progress through filling out a long form or the results of a measurement. A new set of elements defined in HTML5 is aimed at exactly that purpose.

progress

Progress bars show movement toward a set goal and are commonly used in operating systems and on the Web, such as when loading data into a web application or installing software. The progress element gives you a standardized method for implementing progress bars in your own pages. In its simplest form, it shows progress between 0 and 1, using the value attribute to show the current position:

<progress value="0.5">0.5</progress>

Although this element is displayed as a bar in most browsers—as you can see in Figure 8-9—including the value inside the element is also good practice, as it serves as a fallback for browsers that don’t have a graphical widget.

Different implementations of the progress element: Firefox for Android (left) and Opera for Ubuntu (right)
Figure 8-9. Different implementations of the progress element: Firefox for Android (left) and Opera for Ubuntu (right)

If the values you want to use can’t be simply divided into a range of 0 to 1, you can set a different range with the max and min attributes:

<progress max="20" min="10" value="15">15</progress>

Updating the bar using script is easy, requiring only a change of the value attribute, perhaps in a function somewhat like this one, where any value supplied as an argument to the updateProgress function updates the progress bar accordingly:

var progressBar = document.querySelector('progress'),
  updateProgress = function (newValue) {
    progressBar.value = newValue;
};

You can get the current progress toward the target by using the position property, which returns the result of dividing the current value by that of the maximum value; in the case of the previous example, the max attribute is 20 and the value is 15, so the position is 0.75:

var currentProgress = progressBar.position;

meter

At first glance, the meter element seems superficially the same as progress, and indeed, some browsers style the two in the same way. They differ semantically, however; where progress shows movement toward a goal, meter shows a scalar measurement, such as a rating or a poll result. At its most simple, meter shows a value between 0 and 1.0, just like progress:

<meter value="0.5">0.5 of 1</meter>

The similarities to progress continue, as you should add a text child for browsers that don’t represent this graphically, and max and min attributes are also available if you want to change the scale:

<meter max="20" min="10" value="15">15 of 20</meter>

Where meter differs significantly from progress is in displaying ranges of low, medium, and high values, using three new attributes: low sets the upper limit of the low range, high sets the lower limit of the high range, and optimum sets the ideal value. If these attributes are present, the meter’s current value is either displayed as being within acceptable bounds or flagged as being outside them.

The following code example illustrates this. Here, any value less than or equal to 0.2 is considered low and will be displayed with a warning color (often yellow) in many browsers, whereas a value greater than or equal to 0.8 is considered high and will be likewise flagged (most commonly in red); any number between those two will be marked in the standard color (usually green) for the average range:

<meter low="0.2" high="0.8" value="0.65">0.65 of 1</meter>

Introducing the optimum attribute changes the behavior slightly, as it can introduce a third level of “acceptability” depending on where it’s positioned. For example, given the meter element in the previous example, you could say there are three ranges: low for any value of 0.2 or less, high for any 0.8 or greater, and average for any greater than 0.2 but less than 0.8. If you were to set the optimum to be 0.9, any value in the high range would be optimal and colored green, any in the average range less optimal and colored yellow, and any in the low range less optimal still and colored red. Conversely, if the optimum value were 0.1, those ranges of optimality would be reversed.

If that all sounds a little complex, the following markup shows a few different examples, which you can see for yourself in the example file input-types-meter.html and illustrated in Figure 8-10; I advise you to view this example as it relies on color, which is hard to convey in a black-and-white book! I’ll annotate this code and then explain it subsequently.

1 <meter low="0.2" high="0.8" value="0.85">0.85 of 1</meter>
2 <meter low="0.2" high="0.8" optimum="0.9" value="0.85">0.85 of 1</meter>
3 <meter low="0.2" high="0.8" optimum="0.1" value="0.85">0.85 of 1</meter>

In all three examples, the low range is 0 to 0.2, the medium range is 0.21 to 0.79, the high range is 0.8 to 1, and the value of the meter is 0.85. The numerals correspond to the examples in Figure 8-10 in vertical order. In 1 no optimum value is given, so the optimum range is medium (neither high nor low). The value of the meter is in the high range, one range away from optimum, so it’s colored yellow. In 2 the optimum value is 0.9, so the high range becomes optimum; as the value is 0.85, this value is within optimum range and colored green. In 3 the optimum value is 0.1, so the low range becomes optimum; the value is 0.85, which is two ranges away from optimum, so colored red.

Different values for the meter element
Figure 8-10. Different values for the meter element

As I said, this concept is a little hard to convey in black and white, so try the example for yourself. Once you see it, the concept is quite simple and easily grasped.

output

The output element displays the result of a calculation or user input and is especially handy for showing the result of interactions in other fields. A purely semantic element, output has no widget or on-screen presence if no value is supplied. At its most basic, it requires no attributes:

<output></output>

The output element becomes more useful when interacted with using JavaScript. Its key properties are value, which gets or sets the value, and defaultValue, which gets or sets a default value (if none is supplied). To show how this works, I’ve written a short script that updates the output element when a range input is changed. Here’s the markup:

<label for="output">Output</label>
<input type="range" id="range">
<output id="output" for="range"></output>

The following script first selects the elements I’ll interact with, sets a default value of 50 for the output element, and then adds an event listener to the range input, which fires whenever the value is changed. This listener runs an anonymous function to get the value of the input and set the value of the output. Here’s the final script, which you can try for yourself in input-types-output.html:

var range = document.getElementById('range'), output = document.getElementById('output');
output.defaultValue = 50;
range.addEventListener('change', function (e) {
  var newValue = e.currentTarget.value;
  output.value = newValue;
}, false);