Tuesday, 24 May 2011

HTML 5


HTML5 is the next generation of HTML.
What is HTML5?
·         HTML5 will be the new standard for HTML, XHTML, and the HTML DOM.
·         HTML5 is still a work in progress. However, most modern browsers have some HTML5 support.

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.

Browser Support
HTML5 is not yet an official standard, and no browsers have full HTML5 support.
But all major browsers (Safari, Chrome, Firefox, Opera, Internet Explorer) continue to add new HTML5 features to their latest versions.

Note Changes
In addition to the new features, you should also take a note of following important changes:
·         Simple DOCTYPE
HTML5 requires very simple and easy to remember DOCTYPE: <!DOCTYPE html>. It intentionally contains no version, so the DOCTYPE will remain usable for all future revisions of HTML.
·         Easy to Remember Attribute For Language
You are not required to use xmlns or xml:lang attributes in the <html> tag. <html lang=”en”> will now do the trick with HTML5.
·         Easy to Remember Attribute For Character Set
You can now define character encoding, using new “charset” attribute in the meta tag: <meta charset=”utf-8″ />
·         Trailing Slashes Not Required
Void elements in HTML5 (e.g. the br, img and input elements) don’t require a trailing slash.
·         Elements Thrown Away
Following elements are not supported in HTML5: <acronym>, <applet>, <basefont>, <big>, <center>, <dir>, <font>, <frame>, <frameset>, <noframes>, <s>, <strike>, <tt>, <u> and <xmp>

Sample Code:
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>HTML5 Document</title>
</head>
<body>

</body>
</html>

New Elements in HTML5
To better handle today's internet use, HTML5 also includes new elements for better structure, drawing, media content, and better form handling.

New Markup Elements
New elements for better structure:

Tag
Description
<article>
For external content, like text from a news-article, blog, forum, or any other content from an external source
<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 on the Web
Until now, there has never been a standard for showing video on a web page.
Today, most videos are shown through a plugin (like flash). However, not all browsers have the same plugins.
HTML5 specifies a standard way to include video, with the video element.

Video Formats
Currently, there are 3 supported video formats for the video element:

Format
IE
Firefox
Opera
Chrome
Safari
Ogg
No
3.5+
10.5+
5.0+
No
MPEG 4
9.0+
No
No
5.0+
3.0+
WebM
No
4.0+
10.6+
6.0+
No
·         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
The video element allows multiple source elements. Source elements can link to different video files. The browser will use the first recognized format:
<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>
·         The control attribute is for adding play, pause, and volume controls.
·         It is also always a good idea to include the width and height attributes.
·         Insert content between the <video> and </video> tags for browsers that do not support the video element.

All <video> Attributes
Attribute
Value
Description
audio
muted
Defining the default state of 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 on the Web
Until now, there has never been a standard for playing audio on a web page.
Today, most audio are played through a plugin (like flash). However, not all browsers have the same plugins.
HTML5 specifies a standard way to include audio, with the audio element.
The audio element can play sound files, or an audio stream.

Audio Formats
Currently, there are 3 supported formats for the audio element:
Format
IE 9
Firefox 3.5
Opera 10.5
Chrome 3.0
Safari 3.0
Ogg Vorbis
No
Yes
Yes
Yes
No
MP3
Yes
No
No
Yes
Yes
Wav
No
Yes
Yes
No
Yes

<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>
·         The control attribute is for adding play, pause, and volume controls.
·         Insert content between the <audio> and </audio> tags for browsers that do not support the audio element.
·         The audio element allows multiple source elements. Source elements can link to different audio files. The browser will use the first recognized format.

All <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

What is Canvas?
The HTML5 canvas element uses JavaScript to draw graphics on a web page.
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.

Create a Canvas Element
Add a canvas element to the HTML5 page.
Specify the id, width, and height of the element:
The canvas element has no drawing abilities of its own. All drawing must be done inside a JavaScript.
Ex-1: Rectangle
<!DOCTYPE HTML>
<html>
<body>
<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.fillStyle="#147852";
cxt.fillRect(0,0,150,75);
</script>
</body>
</html>
Ex-2: Line
  Consider these two main methods to draw straight lines:
·         moveTo(x,y): Starting point of the line we want to draw. Think of this method as lifting the "pencil" to the specified coordinate.
·         lineTo(x,y): Think of this method as drawing the path from the previous specified point to this one.
<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>
Ex-3: Circle
Syntax: arc(x, y, radius, startAngle, endAngle)
<script type="text/javascript">
var c=document.getElementById("myCanvas");
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>
Ex-4: Gradient
<script type="text/javascript">
var c=document.getElementById("myCanvas");
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>
Ex-5: Image
<script type="text/javascript">
var c=document.getElementById("myCanvas");
var cxt=c.getContext("2d");
var img=new Image();
img.src="img_flwr.png";
cxt.drawImage(img,0,0);
</script>

Ex-6: Drawing Text
Syntax: fillText(text, x, y, [maxWidth]);
<script type="text/javascript">
  var ctx=document.getElementById("mycanvas").getContext('2d');
  ctx.beginPath();
  ctx.textAlign="center";
  ctx.font="italic 50px verdana";
  ctx.fillText("Hello World!", 265, 150);
</script>


Transformations
                Syntax:
·         context.scale(x, y)
·         context.rotate(angle)
·         context.translate(x, y)

<script type="text/javascript">
     var ctx=document.getElementById("mycanvas").getContext('2d');
       ctx.translate(50, 100);
       ctx.rotate(-Math.PI/2);
       ctx.fillStyle = "Red";
                 ctx.mozDrawText("Sample String");
</script>



Storing Data on the Client
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:
<script type="text/javascript">
          localStorage.lastname="Smith";
          document.write(localStorage.lastname);
</script>
The following example counts the number of times a user has visited a page:
<script type="text/javascript">
                   if (localStorage.pagecount)
                   {
                             localStorage.pagecount=Number(localStorage.pagecount) +1;
                   }
                   else
                   {
                             localStorage.pagecount=1;
                   }
                   document.write("Visits "+ localStorage.pagecount + " time(s).");
          </script>

The sessionStorage Object
The sessionStorage object stores the data for one session. The data is deleted when the user closes the browser window.
How to create and access a sessionStorage:
<script type="text/javascript">
                   sessionStorage.lastname="Smith";
                   document.write(sessionStorage.lastname);
          </script>
The following example counts the number of times a user has visited a page, in the current session:
<script type="text/javascript">
          if (sessionStorage.pagecount)
          {
                   sessionStorage.pagecount=Number(sessionStorage.pagecount) +1;
          }
          else
          {
                   sessionStorage.pagecount=1;
          }
          document.write("Visits "+sessionStorage.pagecount+" time(s) this session.");
</script>



HTML 5 Forms
HTML5 New Input Types
HTML5 has several new input types for forms. These new features allow for better input control and validation.
This chapter covers the new input types:
·         email
·         url
·         number
·         range
·         Date pickers (date, month, week, time, datetime, datetime-local)
·         search
·         color

Browser Support
Input type
IE
Firefox
Opera
Chrome
Safari
email
No
4.0
9.0
10.0
No
url
No
4.0
9.0
10.0
No
number
No
No
9.0
7.0
No
range
No
No
9.0
4.0
4.0
Date pickers
No
No
9.0
10.0
No
search
No
4.0
11.0
10.0
No
color
No
No
11.0
No
No

Input Type - email
The email type is used for input fields that should contain an e-mail address.
The value of the email field is automatically validated when the form is submitted.
E-mail: <input type="email" name="user_email" />

Input Type - url
The url type is used for input fields that should contain a URL address.
The value of the url field is automatically validated when the form is submitted.
          Homepage: <input type="url" name="user_url" />

Input Type - 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:
          Points: <input type="number" name="points" min="1" max="10" />
Use the following attributes to specify restrictions for the number type:
Attribute
Value
Description
max
number
Specifies the maximum value allowed
min
number
Specifies the minimum value allowed
step
number
Specifies legal number intervals (if step="3", legal numbers could be -3,0,3,6, etc)
value
number
Specifies the default value

Input Type - 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.
You can also set restrictions on what numbers are accepted:
<input type="range" name="points" min="1" max="10" />
Use the following attributes to specify restrictions for the range type:
Attribute
Value
Description
max
number
Specifies the maximum value allowed
min
number
Specifies the minimum value allowed
step
number
Specifies legal number intervals (if step="3", legal numbers could be -3,0,3,6, etc)
value
number
Specifies the default value

Input Type - 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)
The following example allows you to select a date from a calendar:
          Date: <input type="date" name="user_date" />
Month: <input type="month" name="user_month" />
…etc.

Input Type - search
The search type is used for search fields, like a site search, or Google search.
The search field behaves like a regular text field.

Input Type - color
The color type is used for input fields that should contain a color.
This input type will allow you to select a color from a color picker:
Color: <input type="color" name="user_color" />

HTML5 New Form Elements
HTML5 has several new elements and attributes for forms.
This chapter covers the new form elements:
·         datalist
·         keygen
·         output

Browser Support
Attribute
IE
Firefox
Opera
Chrome
Safari
datalist
No
4.0
9.5
No
No
keygen
No
4.0
10.5
3.0
No
output
No
No
9.5
10.0
No


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.
            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.
<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:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
          function resCalc()
          {
                   numA=document.getElementById("num_a").value;
                   numB=document.getElementById("num_b").value;
                   document.getElementById("result").value=Number(numA)+Number(numB);
          }
</script>
</head>
<body>
          <p>Simple calculator using the output element:</p>
          <form onsubmit="return false">
                    <input id="num_a" /> +
                    <input id="num_b" /> =
                    <output id="result" onforminput="resCalc()"></output>
          </form>

</body>
</html>


HTML5 New Form Attributes
This chapter covers some of the new attributes for <form> and <input>.
New form attributes:
·         autocomplete
·         novalidate
New input attributes:
·         autocomplete
·         autofocus
·         form
·         form overrides (formaction, formenctype, formmethod, formnovalidate, formtarget)
·         height and width
·         list
·         min, max and step
·         multiple
·         pattern (regexp)
·         placeholder
·         required

Browser Support

Attribute
IE
Firefox
Opera
Chrome
Safari
autocomplete
8.0
3.5
9.5
3.0
4.0
autofocus
No
4.0
10.0
3.0
4.0
form
No
4.0
9.5
10.0
No
form overrides
No
4.0
10.5
10.0
No
height and width
8.0
3.5 
9.5
3.0
4.0
list
No
4.0
9.5
No
No
min, max and step
No
No
9.5
3.0
No
multiple
No
3.5
11.0
3.0
4.0
novalidate
No
4.0
11.0
10.0
No
pattern
No
4.0
9.5
3.0
No
placeholder
No
4.0
11.0
3.0
3.0
required
No
4.0
9.5
3.0
No

autocomplete Attribute

The autocomplete attribute specifies that the form or input field should have an autocomplete function.
Note: 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.
            <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 Attribute

The autofocus attribute specifies that a field should automatically get focus when a page is loaded.
Note: The autofocus attribute works with all <input> types.
User name: <input type="text" name="user_name"  autofocus="autofocus" />

form Attribute

The form attribute specifies one or more forms the input field belongs to.
Note: 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" />
Note: To refer to more than one form, use a space-separated list. 

Form Override Attributes

The form override attributes allow you to override some of the attributes set for the form element.
The form override attributes are:
·         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
Note: The form override attributes works with the following <input> types: submit and image.
<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>
Note: These attributes are helpful for creating different submit buttons.

height and width Attributes

The height and width attributes specifies the height and width of the image used for the input type image.
Note: The height and width attributes only works with <input> type: image.
<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.
Note: The list attribute works with the following <input> types: text, search, url, telephone, email, date pickers, number, range, and color.
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>

multiple Attribute

The multiple attribute specifies that multiple values can be selected for an input field.
Note: The multiple attribute works with the following <input> types: email, and file.
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 is a regular expression.
Note: The pattern attribute works with the following <input> types: text, search, url, telephone, email, and password
The example below shows a text field that can only contain three letters (no numbers or special characters).
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.
Note: The placeholder attribute works with the following <input> types: text, search, url, telephone, email, and password.
The hint is displayed in the input field when it is empty, and disappears when the field gets focus.
            <input type="search" name="user_search"  placeholder="Enter Search Text" />

 

required Attribute

The required attribute specifies that an input field must be filled out before submitting.
Note: The required attribute works with the following <input> types: text, search, url, telephone, email, password, date pickers, number, checkbox, radio, and file.
                Name: <input type="text" name="usr_name" required="required" />

1 comment: