HTML5
HTML5 will be the new standard for HTML, XHTML, and the HTML DOM.
HTML5 is cooperation between the World Wide Web Consortium (W3C) and the Web Hypertext Application Technology Working Group (WHATWG).
WHATWG was working with web forms and applications, and W3C was working with XHTML 2.0. In 2006, they decided to cooperate and create a new version of HTML.
Some rules for HTML5 were established:
- New features should be based on HTML, CSS, DOM, and JavaScript
- Reduce the need for external plugins (like Flash)
- Better error handling
- More markup to replace scripting
- HTML5 should be device independent
- The development process should be visible to the public
New Features :
Some of the most interesting new features in HTML5:
- The canvas element for drawing.
- The video and audio elements for media playback.
- Better support for local offline storage.
- New content specific elements, like article, footer, header, nav, section.
- New form controls, like calendar, date, time, email, url, search.
New Markup Elements
New elements for better structure:
Tag | Description |
<article> | Specifies independent, self-contained content, could be a news-article, blog post, forum post, or other articles which can be distributed independently from the rest of the site. |
<aside> | For content aside from the content it is placed in. The aside content should be related to the surrounding content |
<command> | A button, or a radio button, or a checkbox |
<details> | For describing details about a document, or parts of a document |
<summary> | A caption, or summary, inside the details element |
<figure> | For grouping a section of stand-alone content, could be a video |
<figcaption> | The caption of the figure section |
<footer> | For a footer of a document or section, could include the name of the author, the date of the document, contact information, or copyright information |
<header> | For an introduction of a document or section, could include navigation |
<hgroup> | For a section of headings, using <h1> to <h6>, where the largest is the main heading of the section, and the others are sub-headings |
<mark> | For text that should be highlighted |
<meter> | For a measurement, used only if the maximum and minimum values are known |
<nav> | For a section of navigation |
<progress> | The state of a work in progress |
<ruby> | For ruby annotation (Chinese notes or characters) |
<rt> | For explanation of the ruby annotation |
<rp> | What to show browsers that do not support the ruby element |
<section> | For a section in a document. Such as chapters, headers, footers, or any other sections of the document |
<time> | For defining a time or a date, or both |
<wbr> | Word break. For defining a line-break opportunity. |
New Media Elements
HTML5 provides a new standard for media content:
Tag | Description |
<audio> | For multimedia content, sounds, music or other audio streams |
<video> | For video content, such as a movie clip or other video streams |
<source> | For media resources for media elements, defined inside video or audio elements |
<embed> | For embedded content, such as a plug-in |
The Canvas Element
The canvas element uses JavaScript to make drawings on a web page.
Tag | Description |
<canvas> | For making graphics with a script |
New Form Elements
HTML5 offers more form elements, with more functionality:
Tag | Description |
<datalist> | A list of options for input values |
<keygen> | Generate keys to authenticate users |
<output> | For different types of output, such as output written by a script |
New Input Type Attribute Values
Also, the input element's type attribute has many new values, for better input control before sending it to the server:
Type | Description |
tel | The input value is of type telephone number |
search | The input field is a search field |
url | The input value is a URL |
email | The input value is one or more email addresses |
datetime | The input value is a date and/or time |
date | The input value is a date |
month | The input value is a month |
week | The input value is a week |
time | The input value is of type time |
datetime-local | The input value is a local date/time |
number | The input value is a number |
range | The input value is a number in a given range |
color | The input value is a hexadecimal color, like #FF8800 |
Video
HTML5 specifies a standard way to include video, with the video element.
Ogg = Ogg files with Theora video codec and Vorbis audio codec.
MPEG4 = MPEG 4 files with H.264 video codec and AAC audio codec
WebM = WebM files with VP8 video codec and Vorbis audio codec
Example:
<video width="320" height="240" controls="controls">
<source src="movie.ogg" type="video/ogg" />
<source src="movie.mp4" type="video/mp4" />
<source src="movie.webm" type="video/webm" />
Your browser does not support the video tag.
</video>
Example:
<video id="video" src="movie.webm" autoplay controls></video>
document.getElementById("video").play();
<video> Attributes
Attribute | Value | Description |
audio | muted | Defining the default state of the the audio. Currently, only "muted" is allowed |
autoplay | autoplay | If present, then the video will start playing as soon as it is ready |
controls | controls | If present, controls will be displayed, such as a play button |
height | pixels | Sets the height of the video player |
loop | loop | If present, the video will start over again, every time it is finished |
poster | url | Specifies the URL of an image representing the video |
preload | preload | If present, the video will be loaded at page load, and ready to run. Ignored if "autoplay" is present |
src | url | The URL of the video to play |
width | pixels | Sets the width of the video player |
Audio
The audio element can play sound files, or an audio stream. Currently, there are 3 main formats for the audio element:
Format : Ogg Vorbis, MP3, Wav
Ex 1
<audio controls="controls">
<source src="song.ogg" type="audio/ogg" />
<source src="song.mp3" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>
Ex 2
<audio id="audio" src="sound.mp3" controls></audio>
document.getElementById("audio").muted = false;
<audio> Attributes
Attribute | Value | Description |
autoplay | autoplay | Specifies that the audio will start playing as soon as it is ready. |
controls | controls | Specifies that controls will be displayed, such as a play button. |
loop | loop | Specifies that the audio will start playing again (looping) when it reaches the end |
preload | preload | Specifies that the audio will be loaded at page load, and ready to run. Ignored if autoplay is present. |
src | url | Specifies the URL of the audio to play |
Canvas
A canvas is a rectangular area, and you control every pixel of it.
The canvas element has several methods for drawing paths, boxes, circles, characters, and adding images.
Specify the id, width, and height of the element:
<canvas id="myCanvas" width="200" height="100"></canvas>
Draw With JavaScript: The canvas element has no drawing abilities of its own. All drawing must be done inside a JavaScript:
<script type="text/javascript">
var c = document.getElementById("myCanvas");
var cxt = c.getContext("2d");
cxt.fillStyle = "#FF0000";
cxt.fillRect(0, 0, 150, 75);
</script>
Draw a line by specifying where to start, and where to stop:
<canvas id="myCanvas" width="200" height="100" style="border: 1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
var c = document.getElementById("myCanvas");
var cxt = c.getContext("2d");
cxt.moveTo(10, 10);
cxt.lineTo(150, 50);
cxt.lineTo(10, 50);
cxt.stroke();
</script>
Draw a circle by specifying the size, color, and position:
<canvas id="myCanvas1" width="200" height="100" style="border: 1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
var c = document.getElementById("myCanvas1");
var cxt = c.getContext("2d");
cxt.fillStyle = "#FF0000";
cxt.beginPath();
cxt.arc(70, 18, 15, 0, Math.PI * 2, true);
cxt.closePath();
cxt.fill();
</script>
Draw a gradient background with the colors you specify:
<canvas id="myCanvas2" width="200" height="100" style="border: 1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
var c = document.getElementById("myCanvas2");
var cxt = c.getContext("2d");
var grd = cxt.createLinearGradient(0, 0, 175, 50);
grd.addColorStop(0, "#FF0000");
grd.addColorStop(1, "#00FF00");
cxt.fillStyle = grd;
cxt.fillRect(0, 0, 175, 50);
</script>
Draw a curve background with the black colors.
<canvas id="canvas" width="838" height="220"></canvas>
<script>
var canvasContext = document.getElementById("canvas").getContext("2d");
canvasContext.fillRect(250, 25, 150, 100);
canvasContext.beginPath();
canvasContext.arc(450, 110, 100, Math.PI * 1 / 2, Math.PI * 3 / 2);
canvasContext.lineWidth = 15;
canvasContext.lineCap = 'round';
canvasContext.strokeStyle = 'rgba(255, 127, 0, 0.5)';
canvasContext.stroke();
</script>
Web Storage
HTML5 offers two new objects for storing data on the client:
- localStorage - stores data with no time limit
- sessionStorage - stores data for one session
Earlier, this was done with cookies. Cookies are not suitable for large amounts of data, because they are passed on by EVERY request to the server, making it very slow and in-effective.
In HTML5, the data is NOT passed on by every server request, but used ONLY when asked for. It is possible to store large amounts of data without affecting the website's performance.
The data is stored in different areas for different websites, and a website can only access data stored by itself.
HTML5 uses JavaScript to store and access the data.
The localStorage Object
The localStorage object stores the data with no time limit. The data will be available the next day, week, or year.
How to create and access a localStorage:
Example
<script type="text/javascript"> localStorage.lastname = "Smith"; document.write(localStorage.lastname); </script> The sessionStorage ObjectThe sessionStorage object stores the data for one session. The data is deleted when the user closes the browser window.
Example<script type="text/javascript"> sessionStorage.lastname = "Smith"; document.write(sessionStorage.lastname); </script>
|
Input Types
Email : The value of the email field is automatically validated when the form is submitted.
Ex : E-mail: <input type="email" name="user_email" />
Url: The value of the url field is automatically validated when the form is submitted.
Ex: Homepage: <input type="url" name="user_url" />
Number: The number type is used for input fields that should contain a numeric value.
You can also set restrictions on what numbers are accepted:
Ex: Points: <input type="number" name="points" min="1" max="10" />
Range: The range type is used for input fields that should contain a value from a range of numbers.
The range type is displayed as a slider bar.
Ex: <input type="range" name="points" min="1" max="10" />
Date Pickers:
HTML5 has several new input types for selecting date and time:
- date - Selects date, month and year
- month - Selects month and year
- week - Selects week and year
- time - Selects time (hour and minute)
- datetime - Selects time, date, month and year (UTC time)
- datetime-local - Selects time, date, month and year (local time)
Date: <input type="date" name="user_date" />
Month: <input type="month" name="user_date" />
Time: <input type="time" name="user_date" />
Datetime <input type="datetime" name="user_date" />
Search: the Search fields behave like a regular search fields.
Ex: <input type="search" results="10" placeholder="Search..." />
Color: The color type is used for input fields that should contain a color.
Ex: <input type="color" placeholder="e.g. #bbbbbb" />
New Form Elements
New form elements:
Datalist Element:
The datalist element specifies a list of options for an input field.
The list is created with option elements inside the datalist.
To bind a datalist to an input field, let the list attribute of the input field refer to the id of the datalist:
Ex : Webpage: <input type="url" list="url_list" name="link" />
<datalist id="url_list">
<option label="W3Schools" value="http://www.w3schools.com" />
<option label="Google" value="http://www.google.com" />
<option label="Microsoft" value="http://www.microsoft.com" />
</datalist>
keygen Element:
The purpose of the keygen element is to provide a secure way to authenticate users.
The keygen element is a key-pair generator. When a form is submitted, two keys are generated, one private and one public.
The private key is stored on the client, and the public key is sent to the server. The public key could be used to generate a client certificate to authenticate the user in the future.
Currently, the browser support for this element is not good enough to be a useful security standard.
Ex:
<form action="demo_form.asp" method="get">
Username: <input type="text" name="usr_name" />
Encryption: <keygen name="security" />
<input type="submit" />
</form>
output Element:
The output element is used for different types of output, like calculations or script output:
Ex : <output id="result" onforminput="resCalc()"></output>
Form Attributes
autcomplete:
The autocomplete attribute works with <form>, and the following <input> types: text, search, url, telephone, email, password, datepickers, range, and color.
When the user starts to type in an autocomplete field, the browser should display options to fill in the field:
Ex: <form action="demo_form.asp" method="get" autocomplete="on">
First name: <input type="text" name="fname" /><br />
Last name: <input type="text" name="lname" /><br />
E-mail: <input type="email" name="email" autocomplete="off" /><br />
<input type="submit" />
</form>
autofocus:
The autofocus attribute specifies that a field should automatically get focus when a page is loaded. The autofocus attribute works with all <input> types
User name: <input type="text" name="user_name" autofocus="autofocus" />
form Attrubute:
The form attribute specifies one or more forms the input field belongs to. The form attribute works with all <input> types.
The form attribute must refer to the id of the form it belongs to:
<form action="demo_form.asp" method="get" id="user_form">
First name:<input type="text" name="fname" />
<input type="submit" />
</form>
Last name: <input type="text" name="lname" form="user_form" />
Form Override:
- formaction - Overrides the form action attribute
- formenctype - Overrides the form enctype attribute
- formmethod - Overrides the form method attribute
- formnovalidate - Overrides the form novalidate attribute
- formtarget - Overrides the form target attribute
Ex:
<form action="demo_form.asp" method="get" id="user_form">
E-mail: <input type="email" name="userid" /><br />
<input type="submit" value="Submit" /><br />
<input type="submit" formaction="demo_admin.asp" value="Submit as admin" /><br />
<input type="submit" formnovalidate="true" value="Submit without validation" /><br />
</form>
height and width Attributes:
The height and width attributes specifies the height and width of the image used for the input type image.The height and width attributes only works with <input> type: image.
Ex: <input type="image" src="img_submit.gif" width="24" height="24" />
list Attribute:
The list attribute specifies a datalist for an input field. A datalist is a list of options for an input field.
The list attribute works with the following <input> types: text, search, url, telephone, email, date pickers, number, range, and color.
Ex:
Webpage: <input type="url" list="url_list" name="link" />
<datalist id="url_list">
<option label="W3Schools" value="http://www.w3schools.com" />
<option label="Google" value="http://www.google.com" />
<option label="Microsoft" value="http://www.microsoft.com" />
</datalist>
min, max and step Attributes:
The max attribute specifies the maximum value allowed for the input field.
The min attribute specifies the minimum value allowed for the input field.
The step attribute specifies the legal number intervals for the input field (if step="3", legal numbers could be -3,0,3,6, etc).
Points: <input type="number" name="points" min="0" max="10" step="3" />
multiple Attribute:
The multiple attribute specifies that multiple values can be selected for an input field.
Select images: <input type="file" name="img" multiple="multiple" />
Novalidate attribute:
The novalidate attribute specifies that the form or input field should not be validated when submitted.
If this attribute is present the form will not validate form input.
Note: The novalidate attribute works with: <form> and the following <input> types: text, search, url, telephone, email, password, date pickers, range, and color.
<form action="demo_form.asp" novalidate="novalidate">
E-mail: <input type="email" name="user_email" />
<input type="submit" />
</form>
pattern Attribute:
The pattern attribute specifies a pattern used to validate an input field.
The pattern attribute works with the following <input> types: text, search, url, telephone, email, and password
Country code: <input type="text" name="country_code"
pattern="[A-z]{3}" title="Three letter country code" />
placeholder Attribute:
The placeholder attribute provides a hint that describes the expected value of an input field.
Ex : <input type="search" name="user_search" placeholder="Search W3Schools" />
required Attribute:
The required attribute specifies that an input field must be filled out before submitting.
Name: <input type="text" name="usr_name" required="required" />
DRAG: We can drag the thing. Have a look at the following example.
http://html5demos.com/drag
DRAG AND DROP:
<head>
<style>
body
{
font-family: "Arial" , sans-serif;
}
table.userTable
{
width: 40em;
margin: 0 auto;
border-collapse: collapse;
border: solid 1px black;
}
table.userTable thead
{
}
table.userTable th
{
width: 33%;
background: #333333;
color: white;
}
table.userTable tbody td
{
border: solid 1px black;
padding: 1em;
}
table.userTable tfoot td
{
border: none;
visibility: hidden;
}
table.userTable tfoot td
{
border: solid 1px white;
}
table.userTable tfoot td.showHelp
{
border: solid 1px white;
visibility: visible;
}
.userList
{
width: 10em;
height: 10em;
overflow: auto;
border: solid 1px black;
}
table.userTable td a
{
display: block;
width: 10em;
height: 1.4em;
border: solid 1px #999999;
text-decoration: none;
text-align: center;
margin-bottom: .3em;
background-color: white;
cursor: move;
}
.draggedUser
{
zoom: 1;
opacity: .2;
-moz-opacity: .2;
filter: alpha(opacity=20);
}
#unassignedUsers:hover
{
background-color: #ffcccc;
}
#restrictedUsers:hover
{
background-color: #ffffcc;
}
#powerUsers:hover
{
background-color: #ccffcc;
}
</style>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Drag and Drop Example: Permission Form</title>
<link href="css/permission.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="http://www.useragentman.com/shared/js/EventHelpers.js"></script>
<script type="text/javascript" src="http://www.useragentman.com/shared/js/DragDropHelpers.js"></script>
<script type="text/javascript" src="http://www.useragentman.com/test/dragAndDrop/js/permissionForm.js"></script>
</head>
<body>
<form>
<fieldset>
<legend>Permissions</legend>
<p>
To change
a user's credentials, drag and drop the user into the right box.</p>
<table class="userTable">
<thead>
<tr>
<th>
Unassigned Users
</th>
<th>
Restricted Users
</th>
<th>
Power Users
</th>
</tr>
</thead>
<tbody>
<tr>
<td id="unassignedUsers" class="userList">
<a href="#" draggable="true">Moe Howard</a> <a href="#" draggable="true">Curly Howard</a>
<a href="#" draggable="true">Shemp Howard</a> <a href="#" draggable="true">Larry Fine</a>
</td>
<td id="restrictedUsers" class="userList">
</td>
<td id="powerUsers" class="userList">
</td>
</tr>
</tbody>
<tfoot>
<td id="unassignedUsersHelp">
Drag a user from this list to another list to change the user's permissions.
</td>
<td id="restrictedUsersHelp">
Dragging user here will give this user restricted permissions
</td>
<td id="powerUsersHelp">
Dragging a user here will give this user power user access.
</td>
</tfoot>
</table>
</fieldset>
</form>
</body>
CONTENT EDITABLE: We can to edit and change their contents.
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=620" />
<title>HTML5 Demo: ContentEditable</title>
<link rel="stylesheet" href="/css/html5demos.css" type="text/css" />
<script src="/js/h5utils.js"></script>
</head>
<body>
<section id="wrapper">
<header>
<h1>
ContentEditable</h1>
</header>
<article>
<section id="editable" contenteditable="true">
<p>
Here's a typical paragraph element</p>
<ol>
<li>and now a list</li>
<li>with only</li>
<li>three items</li>
</ol>
</section>
<div>
<input type="button" id="clear" value="Clear changes" />
</div>
</article>
<script>
var editable = document.getElementById('editable');
addEvent(editable, 'blur', function () {
// lame that we're hooking the blur event
localStorage.setItem('contenteditable', this.innerHTML);
document.designMode = 'off';
});
addEvent(editable, 'focus', function () {
document.designMode = 'on';
});
addEvent(document.getElementById('clear'), 'click', function () {
localStorage.clear();
window.location = window.location; // refresh
});
if (localStorage.getItem('contenteditable')) {
editable.innerHTML = localStorage.getItem('contenteditable');
}
</script>
<a id="html5badge" href="http://www.w3.org/html/logo/"></a>
</section>
<a href="http://github.com/remy/html5demos"></a> <script src="/js/prettify.packed.js"></script>
<script>
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script>
try {
var pageTracker = _gat._getTracker("UA-1656750-18");
pageTracker._trackPageview();
} catch (err) { }</script>
</body>
</html>
Geolocation:
The geolocation service providers may be able to provide a location to within a few meters. However, in other areas it might be much more than that. All locations are to be considered estimates as there is no guarantee on the accuracy of the locations provided.
Example: