html 5: introduction ehsan toreini islamic azad university, mashhad branch 2012, april

109
HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Upload: linda-ede

Post on 14-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

HTML 5: Introduction

Ehsan ToreiniIslamic Azad University, Mashhad Branch

2012, April

Page 2: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

• You may well ask: "How can I start using HTML5?".• HTML5 ~= HTML + CSS + JS

• You don’t need to throw anything away – builds on HTML4

• It’s easy to get started“Upgrading” to HTML5 can be as simple as changing your doctype.

• It already worksChrome, Firefox, Safari, Opera, IE9, and some mobile browsers

• It’s here to stayW3C more 15 years (XHTML expires & not renewed)

Page 3: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

• 1991 HTML• 1994 HTML 2• 1996 CSS 1 + JavaScript• 1997 HTML 4• 1998 CSS 2• 2000 XHTML 1• 2002 Tableless Web Design• 2005 AJAX• 2006 jQuery• 2009 HTML 5

Page 4: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

How Did HTML5 Get Started?• HTML5 is a 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

Page 5: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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

Page 6: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Introduction

• Important to stay current in HCI• Browsers will eventually replace other UI

platforms

Page 7: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Why HTML5?

• HTML5 and javascript are interpreted– No compilation– Can test UI changes with a console window

Firebug extension for Firefox

Page 8: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Why HTML5?

• Cross-platform– Most browsers (read: not IE) conform to the W3C

standards– The same code works on PC, Mac, Linux

Page 9: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Why HTML5?

• Mobile device support

Page 10: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Why HTML5?

• No need for software updates– Everyone sees the latest version

• Development can be done anywhere– No need for special software– Just a terminal and Firefox/Firebug

Page 11: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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.

Page 12: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

• When will HTML 5 be ready?– Not in few years to come

• Latest version is HTML5– Has all of the power of native applications– Plus more!

Page 13: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

• DOM

When you hit a page

– browser constructs a Document Object Model

(DOM)

– collection of objects represent HTML elements

on the page

Page 14: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April
Page 15: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

• HTML page is like a tree, series of nested elements (tags)

• Root element is <html>:

<html lang="en">

Page 16: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

• 1st Child of the root element is: <head>• Many elements nested – important items

Page 17: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Body Element

• Defines the document’s body. The body element contains all the contents of an HTML document.

Page 18: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

• Many elements nested – important items

Page 19: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Meta Element• Q: I never use funny characters. Do I still need to declare my character encoding (charset)?

• A: Yes! You should always specify a character encoding on every HTML page you serve. Not specifying an encoding can lead to security vulnerabilities.

• UTF-7 is an encoding originally designed for SMTP gateways that couldn't deal with 8-bit/binary characters. It uses a modified Base64 encoding to represent 8-bit characters and ASCII non-printable characters using 7-bit ASCII. The string <script>alert(1)</script> can be encoded in UTF-7 as +ADw-script+AD4-alert(1)+ADw-/script+AD4-

• Generally, a character encoding is set in the Content-Type HTTP Header or a META tag in the HTML itself.

• When the web server does not include an explicit character encoding in its HTTP response, Internet Explorer will attempt to guess the encoding. If certain strings of user input say, +ADw-script+AD4-alert(1)+ADw-/script+AD4- -- are echoed back early enough in the HTML page,Internet Explorer may incorrectly guess that the page is encoded in UTF-7. Suddenly--, the otherwise harmless user input becomes active HTML and will execute.

Page 20: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

• Defines the title of the document (required)• Shown by URL or in Tab

<!DOCTYPE html><html lang="en"><head>

<meta charset="utf-8" />…<title>Worry Basket</title>

</head><body></body></html>

Page 21: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Link Element

• Defines the relationship between a document and an external resource

<!DOCTYPE html><html lang="en"><head>

<meta charset="utf-8" /><title>Worry Basket</title><link rel="stylesheet" href="./css/style-original.css" /><link rel="shortcut icon" href="/favicon.ico" /> <link rel="alternate" type="application/atom+xml"

title="My Weblog feed" href="/feed/" /></head><body></body></html>

Page 22: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

REL = STYLESHEET

• Most frequently used link relationship• Pointing to CSS rules– Stored in a separate file

<link rel="stylesheet" href="style-original.css" type="text/css" />

HTML5 optimization – drop the type attribute – there is only 1 stylesheet language for the web CSS

<link rel="stylesheet" href="style-original.css" />

Page 23: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

REL = SHORTCUT ICON

• Small icon associated with the page

<link rel="shortcut icon" href="/favicon.ico">

• Usually displayed in the browser's location bar next to the URL or Tab

Page 24: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

REL = ALTERNATE

• Quite common, when combined with either the RSS or Atom media type:

“feed autodiscovery”

• It allows syndicated feed readers (like Google Reader) to discover that a site has a news feed

• You can also use other type attribues PDF

<link rel="alternate" type="application/atom+xml" title="My Weblog feed" href="/feed/" />

Page 25: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Full Chart of link relations

Page 26: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

<script> Element

• Used to define a Client-Side script– Either contains scripting statements or points to

an external file<!DOCTYPE html><html lang="en"><head>

<meta charset="utf-8" /><title>Worry Basket</title><link rel="stylesheet" href="./css/style-original.css" /><script type="text/javascript" src="./js/jquery-1.5.1.js"></script>

</head><body></body></html>

Page 27: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

What is Needed?

More Meaning to tags than just Divs

Page 28: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

<header> Element

• Introduction or group of navigation elements

Page 29: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Navigation

• Defines a section intended for navigation

Page 30: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Footer Element

• More specific element for footer of a document

Page 31: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Header Nav

Article AsideHeader

Section

Section

Footer

Figure

Page 32: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

A Form of Madness

• Everybody knows about web forms, right? • Make a <form>• A few <input type="text"> elements, maybe

an<input type="password">• Finish it off with an <input

type="submit"> button• You’re done.

• HTML5 defines over a dozen new input types

Page 33: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

New Input Type

• HTML5 has several new input types for forms. These new features allow for better input control and validation.

• New input types :• email• url• number• range• Date pickers (date, month, week, time, datetime, datetime-

local)• search• color

Page 34: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Browser Support• Browser support for the new input type :

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

Page 35: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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.• Basic syntax :

E-mail: <input type="email" name="user_email" />

Page 36: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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.• Basic syntax :

Homepage: <input type="url" name="user_url" />

Page 37: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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• Example :

Points: <input type="number" name="points" min="1" max="10" />

Page 38: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Input Type - number• 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

Page 39: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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• Example:

<input type="range" name="points" min="1" max="10" />

Page 40: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Input Type - range• 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

Page 41: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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)

• Example:

Date: <input type="date" name="user_date" />

Page 42: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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.

Page 43: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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" />

Page 44: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

New <input> types

1. <input type="search"> for search boxes2. <input type="number"> for spinboxes3. <input type="range"> for sliders4. <input type="color"> for color pickers5. <input type=""tel> for telephone numbers6. <input type="url"> for web addresses7. <input type="email"> for email addresses8. <input type="date"> for calendar date pickers9. <input type="month"> for months10.<input type="week"> for weeks11.<input type="time"> for timestamps12.<input type="datetime"> for precise, absolute date+time stamps13.<input type="datetime-local"> for local dates and times

buttoncheckboxcolordate datetime datetime-local email filehiddenimagemonth number passwordradiorange resetsearchsubmitteltexttime urlweek

Page 45: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

New HTML Form Elements<input type="email" name="user_email" required placeholder="Enter Email Address"/>

<input type="search" />

<input type="url" name="url" required autofocus/>

<input type="tel" name="tel" required/>

<input type="number" name="number" min="1" max="5" step="1" value="3" required/>

<input type="range" name="range" min="1" max="5" step="1" value="3" required/>

<input type="datetime" name="datetime" required/>

<input type="month" name="month" required/>

Page 46: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April
Page 47: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

New Form Element• HTML5 has several new elements and attributes for

forms.• New form types :• datalist• keygen• output

Page 48: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Browser Support• Browser support for the new input type :

Input Type 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

Page 49: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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>

Page 50: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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.

Page 51: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

keygen Element• Example :

<form action="demo_form.asp" method="get">Username: <input type="text" name="usr_name" />Encryption: <keygen name="security" /><input type="submit" /></form>

Page 52: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

output Element• The output element is used for different types of output, like

calculations or script output.• Example :

<output id="result" onforminput="resCalc()"></output>

Page 53: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Form Validation

• Determining whether a random string of characters is a valid email address is unbelievably complicated.

• The harder you look, the more complicated it gets. • Isn’t it be easier to offload the entire headache to

your browser?

• Addition of the pattern attribute to all the various form fields to which validation applies

<p>Price: <input type="text" pattern="\d+(\.\d{2})?" required></p>

Page 54: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

New Form Attributes• HTML5 has several new elements and attributes for forms.

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

Page 55: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Browser Support• Browser support for the new input type :

Input Type 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

Page 56: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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>

• In some browsers you may need to activate the autocomplete function for this to work.

Page 57: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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" />

Page 58: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

form Attribute• The form attribute specifies one or more forms the input field belongs

to.• Note: The form attribute works with all <input> types.• To refer to more than one form, use a space-separated list. • 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" />

Page 59: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Form Override Attribute• 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.

Page 60: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Form Override Attribute• Example :

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

• These attributes are helpful for creating different submit buttons.

Page 61: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

height and width Attribute• 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.

<input type="image" src="img_submit.gif" width="24" height="24" />

Page 62: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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.

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>

Page 63: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

min, max and step Attribute• The min, max and step attributes are used to specify

restrictions for input types containing numbers or dates.• 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).

Page 64: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

min, max and step Attribute• The min, max, and step attributes works with the following

<input> types: date pickers, number, and range.• The example below shows a numeric field that accepts values

between 0 and 10, with a step of 3 (legal numbers are 0, 3, 6 and 9):

Points: <input type="number" name="points" min="0" max="10" step="3" />

Page 65: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

multiple Attribute• The multiple attribute specifies that multiple values can be

selected for an input field.• The multiple attribute works with the following <input>

types: email, and file.• Example :

Select images: <input type="file" name="img" multiple="multiple" />

Page 66: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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.• The novalidate attribute works with: <form> and the

following <input> types: text, search, url, telephone, email, password, date pickers, range, and color.

• Example :

<form action="demo_form.asp" novalidate="novalidate">E-mail: <input type="email" name="user_email" /><input type="submit" /></form>

Page 67: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

pattern Attribute• The pattern attribute specifies a pattern used to validate an

input field.• The pattern is a regular expression. • 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" />

Page 68: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

placeholder Attribute• The placeholder attribute provides a hint that describes the

expected value of an input field.• 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.• Example :

<input type="search" name="user_search"  placeholder="Search W3Schools" />

Page 69: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

required Attribute• HTML5 form validation isn’t limited to the type of each field. • The required attribute specifies that an input field must be

filled out before submitting.• The required attribute works with the following <input>

types: text, search, url, telephone, email, password, date pickers, number, checkbox, radio, and file.

• Example :

Name: <input type="text" name="usr_name" required="required" />

Page 70: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Required Fields

<p>Website: <input type="url" name="website" required></p><style> [required] { border-color: #88a; -webkit-box-shadow: 0 0 3px rgba(0, 0, 255, .5); } :invalid { border-color: #e88; -webkit-box-shadow: 0 0 5px rgba(255, 0, 0, .8); }</style>

Page 71: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Video• HTML 5.0 provides a standard for showing video. Using

the <video> element we can easily embed video within our web page.

• The video formats the <video> element supports include the following : • MPG4 (with H.264 video codec and AAC audio

codec)• OGG (with Thedora video codec and Vorbis audio

codec)

Page 72: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Format• 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.

• Currently, there are 2 supported video formats for the video element:

Format Firefox 3.5 Opera 10.5 Chrome 3.0 IE 8 Safari 3.0

Ogg Yes Yes Yes No No

MPEG 4 No No Yes No Yes

Page 73: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Basic SyntaxBasic Syntax to Embed Video :

<video src="movie.ogg" controls="controls">*Content we place in between the tags will be displayed when the browser doesn't support displaying video

</video>

• We can use the width and height attributes in order to specify the size

• The control attribute is for adding the play, pause and volume controls

Page 74: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Basic SyntaxExample :

<h1>HTML 5 Playing Video Sample</h1>

<videosrc="http://example.com/video/example_video.ogg"controls="controls"width=“600"height=“280">

Browser does not support HTML 5.0

</video>

Page 75: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Basic Syntax• 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>

Page 76: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Attribute• The HTML 5.0 supports the following 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

Page 77: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Audio• The HTML 5.0 specification allows us playing sound

using the <audio> element.• The <audio> element can play sound files or an audio

stream.• The HTML 5.0 specification aims at supporting the

following sounds formats: MP3, WAV and Ogg Vorbis.

Page 78: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Format• 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.

• Currently, there are 3 supported audio formats for the audio element:

Format Firefox 3.5 Opera 10.5 Chrome 3.0 IE 9 Safari 3.0

Ogg Vorbis Yes Yes Yes No No

MP3 No No Yes Yes Yes

Wav Yes Yes No No Yes

Page 79: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Basic SyntaxBasic Syntax to Embed Video :

<audio src="music.ogg" controls="controls">*Content we place in between the tags will be displayed when the browser doesn't support playing audio

</audio>

• The control attribute is for adding the play, pause and volume controls

Page 80: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Basic Syntax• The audio element allows multiple source elements.

Source elements can link to different audio files.• We can add the <source> child elements in between

the audio element tags. The browser will use the first supported format.

<audio controls="controls"><source src="mymusic.ogg" type="audio/ogg"><source src="mymusic.mp3" type="audio/mpeg">

browser does not support html 5.0</audio>

Page 81: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Attribute• The HTML 5.0 supports the following 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 preloadSpecifies 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

Page 82: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Media Events

Page 83: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Media Events

Page 84: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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.

• 2D canvas acceleration uses the same GPU process infrastructure

Page 85: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Basic Syntax• Basic Syntax to create canvas :<canvas id="my_canvas" width="800" height="600"></canvas>

• Once the Canvas was created we can draw various graphics by calling various JavaScript methods on its context.

...<script type="text/javascript">

var c=document.getElementById("my_canvas");var context=c.getContext("2d");context.fillStyle="#FFAA00";context.fillRect(0,0,120,80);

</script>...

Page 86: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Basic Syntax• Example :

<html><head>

<title>Canvas Demo</title></head><body>

<canvas id="my_canvas" width="800" height="600"></canvas><script type="text/javascript">var c=document.getElementById("my_canvas");var context=c.getContext("2d");context.fillStyle="#FFAA00";context.fillRect(0,0,120,80);</script>

</body></html>

Page 87: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

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

• JavaScript uses the id to find the canvas element :var c=document.getElementById("myCanvas");

Page 88: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Using JavaScript, continued…• Then, create a context object :

var cxt=c.getContext("2d");

• The getContext("2d") object is a built-in HTML5 object, with many methods to draw paths, boxes, circles, characters, images and more.

• The next two lines draws a red rectangle:cxt.fillStyle="#FF0000";cxt.fillRect(0,0,150,75);

• The fillStyle method makes it red, and the fillRect method specifies the shape, position, and size.

Page 89: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Understanding Coordinates

• The fillRect method above had the parameters (0,0,150,75).

• This means: Draw a 150x75 rectangle on the canvas, starting at the top left corner (0,0).

• The canvas X and Y coordinates are used to position drawings on the canvas.

Page 90: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Attribute

• The HTML 5.0 supports the following attributes :

Attribute Value Description

height pixels Sets the height of the canvas

width pixels Sets the width of the canvas

Page 91: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Storage

• Local Storage is intended to be used for storing and retrieving data in html pages from the same domain.

• The data can be retrieved from all the windows in the same domain even if the browser is restarted.

• Session Storage is the other Web Storage option and the data is available only in the window it was stored in and is lost when the browser window is closed.

• Aims to be a replacement of the cookies- cookies are accessible from both client and server side (4k max)- storage is accessible only from client side (5 – 10 MB)

Page 92: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Storage

• 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.

Page 93: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

The localStorage Object• The localStorage object stores the data with no time

limit.

• Example :

<script type="text/javascript">localStorage.lastname="Smith";document.write(localStorage.lastname);</script>

Page 94: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

The localStorage Object• 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>

Page 95: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

The sessionStorage Object• The 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>

Page 96: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

The sessionStorage Object• 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>

Page 97: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Other Local Storage, Web SQL Database

• Can store relational data locally in the browser

• Especially useful for offline apps (more later)

Page 98: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

• Web SQL Database– a spec that brings SQL to the client side– specification is based around SQLite

• Show Example: TwitterLocalSQLdb

var db = openDatabase('mydb', '1.0', 'my first database', 2 * 1024 * 1024);db.transaction(function (tx) { // here be the transaction // do SQL magic here using the tx object});db.transaction(function (tx) { tx.executeSql('CREATE TABLE foo (id unique, text)');});tx.executeSql('INSERT INTO foo (id, text) VALUES (?, ?)', [id, userValue]);

Page 99: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Geolocation• Geolocation is the art of figuring out where you are in the world

and (optionally) sharing that information with people you trust.

• How? Chrome and Firefox use your IP and Google Location Services

if (Modernizr.geolocation) {initiate_geolocation();

}

function initiate_geolocation() {

navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_geo_errors); }

function handle_geo_errors(error) { }

function handle_geolocation_query(position){ //alert('Lat: ' + position.coords.latitude + // ' Lon: ' + position.coords.longitude); userPositionLong = position.coords.longitude;userPositionLat = position.coords.latitude;

}

Page 100: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Web Workers

• Web Workers provide a standard way for browsers to run JavaScript in the background.

• With web workers, you can spawn multiple “threads” that all run at the same time, more or less. (Think of how your computer can run multiple applications at the same time, and you’re most of the way there.)

• These “background threads” can do complex mathematical calculations, make network requests, or access local storage while the main web page responds to the user scrolling, clicking, or typing.

Page 101: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Web Sockets

• One of the cool new features of HTML5 is WebSockets, which let us talk to the server without using AJAX requests.

• Websockets can replace long-polling. This is an interesting concept; the client sends a request to the server – now, rather than the server responding with data it may not have, it essentially keeps the connection open until the fresh, up-to-date data is ready to be sent – the client next receives this, and sends another request.

• WebSockets is a technique for two-way communication over one (TCP) socket, a type of PUSH technology. At the moment, it’s still being standardized by the W3C; however, the latest versions of Chrome and Safari have support for WebSockets.

Page 102: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

File / Hardware Access

• Native Drag & Drop• Desktop Drag-In (File API)– Drag files in from the desktop

• Desktop Drag-Out– Drag files out onto the desktop

• FileSystem APIs– Asynchronously write a file to a sandboxed file system

using JavaScript

• Geolocation• Device Orientation• Speech Input

Page 103: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Working with Files

• Using Files in Web Applications

Page 104: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Markup Elements

New elements in HTML 5:

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 radiobutton, 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

Page 105: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Markup Elements

more elements in HTML 5:

Tag Description

<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

Page 106: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Markup Elements

more elements in HTML 5:

Tag Description

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

Page 107: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Markup Elements

New media elements in HTML 5:

Canvas elements in HTML 5:

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

Tag Description

<canvas> For making graphics with a script

Page 108: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Markup Elements

New form elements in HTML 5:

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

Page 109: HTML 5: Introduction Ehsan Toreini Islamic Azad University, Mashhad Branch 2012, April

Markup Elements

New Input Type Attribute Values :

Tag 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