HTML 5 Pros & Cons
Design Principles
Principles should be balanced against each other
View More: http://dev.w3.org/html5/html-design-principles/
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Example document</title>
</head>
<body>
<p>Example paragraph</p>
</body>
</html>
XML syntax
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example document</title>
</head>
<body>
<p>Example paragraph</p>
</body>
</html>
The Modes (Modes for text/html Content)
Quirks Mode
Standards Mode (HTML5 calls it “no quirks mode”)
Almost Standards Mode (HTML5 calls it “limited quirks mode”)
IE7 Mode
IE8 Standards Mode
IE8 Almost Standards Mode
The Effects
Layout
Parsing
Scripting
More Info for [doctype]
<!doctype html>
<title>SVG in text/html</title>
<p>
A green circle:
<svg> <circle r="50" cx="50" cy="50" fill="green"/> </svg>
</p>
HTML5 Language
HTML5 Language-New Elements
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Example document</title>
</head>
<body>
<table border="1">
<tr>
<th>
Element
</th>
<th>
Demo
</th>
</tr>
<tr>
<td>
mark
</td>
<td>
<mark>This is marked text</mark>
</td>
</tr>
<tr>
<td>
progress
</td>
<td>
<progress></progress>
<br>
<progress value="0" max="100" min="0" id="pro"/>
<script type="text/javascript">
var prog=0;
var myprogress = document.getElementById('pro');
function myhandler(){
prog++;
myprogress.value=prog;
if(prog>=100)
clearInterval(timer);
}
var timer = setInterval(myhandler,100);
</script>
</td>
</tr>
<tr>
<td>
meter
</td>
<td>
<meter value="2" min="0" max="10">2 out of 10</meter><br />
<meter value="0.6">60%</meter>
</td>
</tr>
<tr>
<td>
time
</td>
<td>
<p>
We open at <time>10:00</time> every morning.
</p>
<p>
I have a date on <time datetime="2008-02-14">Valentines day</time>
</p>
</td>
</tr>
<tr>
<td>
ruby
</td>
<td>
<ruby>
Ruby<rt>['ru:bi]</rt>
</ruby>
</td>
</tr>
<tr>
<td>
bdo
</td>
<td>
<bdo dir="rtl">
Here is some text written from right-to-left.
</bdo>
</td>
</tr>
<tr>
<td>
wbr
</td>
<td>
<div style="width:200px">
To learn AJAX, you must be familiar with the XML<wbr>Http<wbr>Request (XMLHttpRequest) Object.
</div>
</td>
</tr>
<tr>
<td>
canvas
</td>
<td>
<canvas id="myCanvas">your browser does not support the canvas tag </canvas>
<script type="text/javascript">
var canvas=document.getElementById('myCanvas');
var ctx=canvas.getContext('2d');
ctx.fillStyle='#FF0000';
ctx.fillRect(0,0,80,100);
</script>
</td>
</tr>
<tr>
<td>
details
</td>
<td>
<details>
<summary style="background-color:#1183f8; width:50%">HTML 5</summary>
<ul>
<li>
This is test 1
</li>
<li>
This is test 2
</li>
<li>
This is test 3
</li>
<li>
This is test 4
</li>
</ul>
</details>
</td>
</tr>
<tr>
<td>
datalist(FireFox)
</td>
<td>
<input list="browsers"> <datalist id="browsers"> <option value="Safari"> <option value="Internet Explorer"> <option value="Opera"> <option value="Firefox"> </datalist>
</td>
</tr>
<tr>
<td>
keygen
</td>
<td>
<a href="http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_keygen" target="_blank">keygen demo</a>
</td>
</tr>
</table>
</body>
</html>
Please open demo in Chrome
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Example document</title>
</head>
<body>
<table border="1">
<tr>
<th>
Type
</th>
<th>
Demo
</th>
</tr>
<tr>
<td>
tel
</td>
<td>
<form action="demoTel">
Mobile: <input type="tel" name="user_mobile" title="This is title attribute for tel input" /><br />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
search
</td>
<td>
<form action="demoSearch" title="search title">
Search: <input type="search" name="keywords" placeholder="input your keywords here" required /><br />
<input type="submit" />
</form>
<!--another input <input form="demoSearch" name="default" value="search" />-->
</td>
</tr>
<tr>
<td>
url
</td>
<td>
<form action="demoUrl">
Input your url: <input type="url" name="urlname" placeholder="http://www.ebay.com" autofocus=true /><br />
<input type="submit" />
</form>
<form action="demoUrl" novalidate="novalidate">
Input your url (novalidate): <input type="url" name="urlname" placeholder="http://www.ebay.com" autofocus=true /><br />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
email
</td>
<td>
<form action="">
Email: <input type="email" name="email" placeholder="ywang2@ebay.com" autofocus=true form="demoEmail demoSearch demoUrl" /><br />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
datetime
</td>
<td>
<form action="demo">
Datetime: <input type="datetime" name="dt" placeholder="2011-09-06T00:28Z" /><br />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
date
</td>
<td>
<form action="demo">
Date: <input type="date" name="date" placeholder="2011-09-06" /><br />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
month
</td>
<td>
<form action="demo">
Month: <input type="month" name="m" placeholder="2011-10" /><br />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
week
</td>
<td>
<form action="demo">
Week: <input type="week" name="w" placeholder=2011-W37 /><br />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
time
</td>
<td>
<form action="demo">
Time: <input type="time" name="time" placeholder="08:32" /><br />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
datetime-local
</td>
<td>
<form action="demo">
<input type="datetime-local" name="dtl" /><br />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
number
</td>
<td>
<form action="demo">
<input type="number" name="number" min=0 max=10 step=2 value=3 /><br />
min=0 max=10 step=2
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
range
</td>
<td>
<form action="demo">
<input type="range" name="range" min=0 max=30 step=1 value=0 /><br />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
color
</td>
<td>
<form action="demo">
<input type="color" name="color" /><br />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
fieldset (disabled works in FF)
</td>
<td>
<form>
<fieldset name="flds" disabled="disabled" >
<legend>Information:</legend>
Name: <input type="text" /><br />
Email: <input type="text" /><br />
Date of birth: <input type="text" />
</fieldset>
</form>
</td>
</tr>
<tr>
<td>
pattern
</td>
<td>
<form action="demo">
Input with regular expression
<input id=��ptn�� pattern=^\d{4}\-\d{1,2}-\d{1,2} placeholder="2011-09-09" />
<input type="submit" />
</form>
</td>
</tr>
</table>
</body>
</html>
HTML5 Language-New Attributes
New Attribute | Apply to |
media | a, area |
hreflang, type and rel | area |
target | base |
charset | meta |
autofocus | input (except when the type attribute is hidden), select, textarea and button |
placeholder | input and textarea |
form | input, output, select, textarea, button, label, object and fieldset |
required | input (except when the type attribute is hidden, image or some button type such as submit), select, textarea |
autocomplete ( In some browsers you may need to activate the autocomplete function for this to work.), min, max, multiple(types: email, and file),list,pattern and step | input |
maxlength and wrap | textarea |
formaction, formenctype, formmethod, formnovalidate, and formtarget | input and button (override same attributes on the form element) |
scoped (Style rules within such a style element only apply to the local tree) | style |
async | script (async="true": execute asynchrously with the rest of the page, so the script will be executed while the page continues the parsing. async="false"&defer="true": execute when the page is finished with the parsing. async="false"&defer="false": execute immediately, and the page will wait for the script to finish before continuing the parsing.) |
reversed (list order is descending) | ol |
accesskey, class, dir, id, lang, style, tabindex and title | global attributes |
HTML5 Language-Changed & Absent
HTML5 Microdata
<div itemscope>
<p>Name: <span itemprop="name">Amanda</span></p>
<p>Band: <span itemprop="band" itemscope> <span itemprop="name">Jazz Band</span> (<span itemprop="size">12</span> players)</span></p>
</div>
The outer item here has two properties, "name" and "band". The "name" is "Amanda", and the "band" is an item in its own right, with two properties, "name" and "size". The "name" of the band is "Jazz Band", and the "size" is "12".
<!DOCTYPE HTML>
<html>
<head>
<title>Photo gallery</title>
</head>
<body>
<h1>My photos</h1>
<figure itemscope itemtype="http://n.whatwg.org/work" itemref="licenses">
<img itemprop="work" src="images/house.jpeg" alt="A white house, boarded up, sits in a forest.">
<figcaption itemprop="title">The house I found.</figcaption>
</figure>
<figure itemscope itemtype="http://n.whatwg.org/work" itemref="licenses">
<img itemprop="work" src="images/mailbox.jpeg" alt="Outside the house is a mailbox. It has a leaflet inside.">
<figcaption itemprop="title">The mailbox.</figcaption>
</figure>
<footer>
<p id="licenses">All images licensed under the <a itemprop="license"
href="http://www.opensource.org/licenses/mit-license.php">MIT
license</a>.</p>
</footer>
</body>
</html>
The results in two items with the type "http://n.whatwg.org/work", one with:
work
images/house.jpeg
title
The house I found.
license
http://www.opensource.org/licenses/mit-license.php
...and one with:
work
images/mailbox.jpeg
title
The mailbox.
license
http://www.opensource.org/licenses/mit-license.php
HTML5 Offline Web applications
Non-normative
<!-- clock.html -->
<!DOCTYPE HTML>
<html>
<head>
<title>Clock</title>
<script src="clock.js"></script>
<link rel="stylesheet" href="clock.css">
</head>
<body>
<p>The time is: <output id="clock"></output></p>
</body>
</html>
/* clock.css */
output { font: 2em sans-serif; }
/* clock.js */
setTimeout(function () {
document.getElementById('clock').value = new Date();
}, 1000);
The author can instead provide a manifest of the three files, say "clock.appcache":
CACHE MANIFEST
clock.html
clock.css
clock.js
<!-- clock.html -->
<!DOCTYPE HTML>
<html manifest="clock.appcache">
<head>
<title>Clock</title>
<script src="clock.js"></script>
<link rel="stylesheet" href="clock.css">
</head>
<body>
<p>The time is: <output id="clock"></output></p>
</body>
</html>
With the exception of "no-store" directive, HTTP cache headers and restrictions on caching pages served over TLS (encrypted, using https:) are overridden by manifests. Thus, pages will not expire from an application cache before the user agent has updated it, and even applications served over TLS can be made to work offline.
HTML5 Web workers
Example:
<!DOCTYPE HTML>
<html>
<head>
<title>Worker example: One-core computation</title>
</head>
<body>
<p>The highest prime number discovered so far is: <output id="result"></output></p>
<script>
var worker = new Worker('worker.js');
worker.onmessage = function (event) {
document.getElementById('result').textContent = event.data;
};
</script>
</body>
</html>
Example:
The worker itself is as follows:
var n = 1;
search: while (true) {
n += 1;
for (var i = 2; i <= Math.sqrt(n); i += 1)
if (n % i == 0)
continue search;
// found a prime!
postMessage(n);
}
var source = new EventSource('updates.cgi');
source.onmessage = function (event) { alert(event.data); };
On the server-side, the script ("updates.cgi" in this case) sends messages in the following form, with the text/event-stream MIME type:
data: This is the first message.
data: This is the second message, it
data: has two lines.
data: This is the third message. onopen | open |
onmessage | |
onerror | error |
onclose | close |
var socket = new WebSocket('ws://game.example.com:12010/updates');
socket.onopen = function () {
setInterval(function() {
if (socket.bufferedAmount == 0)
socket.send(getUpdateData());
}, 50);
};
Document A:
var o = document.getElementsByTagName('iframe')[0]; o.contentWindow.postMessage('Hello world', 'http://b.example.org/');
Document B:
window.addEventListener('message', receiver, false);
function receiver(e) {
if (e.origin == 'http://example.com') {
if (e.data == 'Hello world') { e.source.postMessage('Hello', e.origin); }
else { alert(e.data); }
}
}
channel = new MessageChannel()
Returns a new MessageChannel object with two new MessagePort objects.
channel . port1
Returns the first MessagePort object.
channel . port2
Returns the second MessagePort object.
One of the ports is kept as the local port, and the other port is sent to the remote code, e.g. using postMessage():
otherWindow.postMessage('hello', 'http://example.com', [channel.port2]);
To send messages, the postMessage() method on the port is used:
channel.port1.postMessage('hello');
To receive messages, one listens to message events:
channel.port1.onmessage = handleMessage;
function handleMessage(event) {
// message is in event.data
// ...
}
HTML5 Web storageinterface Storage {
readonly attribute unsigned long length;
DOMString? key(unsigned long index);
getter DOMString getItem(DOMString key);
setter creator void setItem(DOMString key, DOMString value);
deleter void removeItem(DOMString key);
void clear();
};
function showMap(position) {
// Show a map centered at (position.coords.latitude, position.coords.longitude).
}
// One-shot position request.
navigator.geolocation.getCurrentPosition(showMap);
CSS3
Resources
Here is PDF version of this topic. HTML5 CSS3 Introduction
Please be noted that some examples do not work in this document.