PRO HTML5 PROGRAMMING POWERFUL APIS FOR RICHER INTERNET APPLICATION DEVELOPMENT.

Slides:



Advertisements
Similar presentations
WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
Advertisements

HTML5, OVERVIEW AND NEW FEATURES PowerPoint by Mason O’Mara.
CHAP 6. WEBSOCKET API.  In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page.
HTML 5. What is HTML5? HTML5 will be the new standard for HTML, XHTML, and the HTML DOM. The previous version of HTML came in The web has changed.
HTML 5 Previous version: HTML4.01, 1999 Still work in progress, most browsers support some of it.
 To publish information for global distribution, one needs a universally understood language, a kind of publishing mother tongue that all computers may.
HTML 5 Tutorial Chapter 1 Introduction. What is HTML5? HTML5 will be the new standard for HTML, XHTML, and the HTML DOM. The previous version of HTML.
HTML 5 The next generation of web programming. WHERE IT ALL BEGAN.
HTML Advanced: HTML 5. Welcome This slideshow presentation is designed to introduce you to HTML 5. It is the third of three HTML workshops available at.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
Bartosz Kowalski Software Developer CERN. Presentation outline -HTML : introduction and history -HTML5 : -History and philosophy -New features -Structure.
Lecture 18. HTML5 and JavaScript Instructor: Jie Yang Department of Computer Science University of Massachusetts Lowell Exploring the Internet,
Chapter 14 Introduction to HTML
Web Design Basic Concepts.
Session: 11. © Aptech Ltd. 2HTML5 Audio and Video / Session 11  Describe the need for multimedia in HTML5  List the supported media types in HTML5 
Chapter 11 Adding Media and Interactivity. Flash is a software program that allows you to create low-bandwidth, high-quality animations and interactive.
Video on the Web. The Evolution of web video formats… WebM (Supported by Google) Ogg (Supported by Theora) Mp4 (h264 video encoding) WebM (Supported by.
Web Programming Language Week 13 Ken Cosh HTML 5.
Cs413_design01.ppt Web Design vs. GUI Design Designing for the Web: the designer has to give up full control of the design In traditional design, you control.
PRO HTML5 PROGRAMMING POWERFUL APIS FOR RICHER INTERNET APPLICATION DEVELOPMENT.
HTML5. What is HTML5? HTML5 will be the new standard for HTML. HTML5 is the next generation of HTML. HTML5 is still a work in progress. However, the major.
HTML 5 New Standardization of HTML. I NTRODUCTION HTML5 is The New HTML Standard, New Elements New Attributes Full CSS3 Support Video and Audio 2D/3D.
HTML5 Accessibility Ted Drake, Yahoo! Accessibility Lab Slideshare.net/7mary4.
Computer Concepts 2014 Chapter 7 The Web and .
What is Web Design?  Web design is the creation of a Web page using hypertext or hypermedia to be viewed on the World Wide Web.
CHAP 4. GEOLOCATION API.  You can request users to share their location and, if they agree, you can provide them with instructions on how to get to a.
PRO HTML5 PROGRAMMING POWERFUL APIS FOR RICHER INTERNET APPLICATION DEVELOPMENT.
Lesson 4: Using HTML5 Markup.  The distinguishing characteristics of HTML5 syntax  The new HTML5 sectioning elements  Adding support for HTML5 elements.
JavaScript, Fourth Edition Chapter 12 Updating Web Pages with AJAX.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Ins & Outs of. CSS3 is Modular Can we use it now?
HTML Advanced: HTML 5. Introduction to HTML 5 These slides are based on source material found at the w3schools.com website.
Getting Started with Canvas IDIA Spring 2013 Bridget M. Blodgett.
Proglan Session 1. .  HTML5 will be the new standard for HTML.  The previous version of HTML, HTML 4.01, came in The web has changed a lot since.
Congratulations! You are part of a global community of thousands of web developers who attend Web Camps to keep their web development skills up-to-date.
XHTML By Trevor Adams. Topics Covered XHTML eXtensible HyperText Mark-up Language The beginning – HTML Web Standards Concept and syntax Elements (tags)
Session: 1. © Aptech Ltd. 2Introduction to the Web / Session 1  Explain the evolution of HTML  Explain the page structure used by HTML  List the drawbacks.
UPLOAD / DOWNLOAD april  HTML5 is just the next iteration of HTML  Previous version was technically HTML 4.01, which incorporated XHTML 1.0.
CHAPTER 15 HTML 5 VIDEO AND AUDIO Intro to HTML5 1.
Semantics vs. Style Importance of Standards Why we follow the rules.
Introduction to HTML. _______________________________________________________________________________________________________________ 2 Outline Key issues.
Objective: To describe the evolution of the Internet and the Web. Explain the need for web standards. Describe universal design. Identify benefits of accessible.
Chapter 11 Adding Media and Interactivity. Chapter 11 Lessons Introduction 1.Add and modify Flash objects 2.Add rollover images 3.Add behaviors 4.Add.
Web Technology (NCS-504) Prepared By Mr. Abhishek Kesharwani Assistant Professor,UCER Naini,Allahabad.
Can SAS Do Canvas? Creating Graphs Using HTML 5 Canvas Elements Edwin J. van Stein Astellas Pharma Global Development.
07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University.
What is HTML5? HTML5 will be the new standard for HTML. The previous version of HTML, HTML 4.01, came in The web has changed a lot since then. HTML5.
Web Design Principles 5 th Edition Chapter 3 Writing HTML for the Modern Web.
CSCI 3100 Tutorial 2 Web Development Tools 1 HTML 5 & CSS 3 1.
Lecture 9: Extra Features and Web Design Tarik Booker CS 120 California State University, Los Angeles.
HTML 5 (Part 1) – Start from SCRATCH. HTML 5 – Start from SCRATCH.
Agenda * What is HTML5 -- Its history and motivation * HTML/XHTML as Human / Machine Readable Format * HTML and its related technologies * Brief summary.
CHAP 2. CANVAS API.  Dynamically generate and render graphics, charts, images and animations  SVG (Scalable Vector Graphics) vs. Canvas  Bitmap canvas.
XHTML. What Is XHTML? XHTML stands for EXtensible HyperText Markup Language XHTML is almost identical to HTML XHTML is stricter than HTML XHTML is HTML.
Chapter 9 HTML 5 Video and Audio
The HTML5 logo was introduced by W3C in 2010
Chap 4. Geolocation API.
Objective % Select and utilize tools to design and develop websites.
Internet of the Past.
Chap 3. Audio/Video api.
HTML5 – The Saga Continues
Data Virtualization Tutorial… CORS and CIS
Web Programming Language
Application with Cross-Platform GUI
Objective % Select and utilize tools to design and develop websites.
The Canvas.
Browser Support for HTML5
HTML 5 Training HTML 5 SYMANTICS [Notes to trainer:
Understand basic HTML and CSS terminology, concepts, and basic operations. Objective 3.01.
Presentation transcript:

PRO HTML5 PROGRAMMING POWERFUL APIS FOR RICHER INTERNET APPLICATION DEVELOPMENT

CHAP 1. OVERVIEW OF HTML5

 HTML was first published as an Internet draft in  Version 2.0, 3.2, 4.0 occurred in the same year, and finally 4.01 in  HTML5 specification was created by Web Hypertext Application Working Group (WHATWG) in  W3C became involved with HTML again in 2006 and published first working draft in 2008, and XHTML 2 stopped in HISTORY OF HTML5

 HTML5 spec. is a working draft today (not final).  2012: candidate recommendation. (HTML5 will be complete )  2022: proposed recommendation.  IE6 does not support many of new features.  Microsoft promises to design that browser with increased HTML5 support. MYTH OF 2022

 Web Hypertext Application Technology Working Group (WHATWG)  Founded in 2004  Vendors: Apple, Mozilla, Google, Opera  World Wide Web Consortium (W3C)  Internet Engineering Task Force (IETF)  HTML5 defines a new WebSocket API that relies on a new WebSocket protocol. WHO IS DEVELOPING HTML5

 Compatibility  Utility - Separation of Presentation and Content  Most of presentational features of earlier versions of HTML are no longer supported (but will still work!).  Shortages of older versions of HTML  Poor accessibility  Unnecessary complexity (Harder to read code)  Large document size – slower loading pages  Interoperability - Simplify wherever possible  Native browser ability instead of complex JavaScript code  A new, simplified character set declaration  Powerful yet simple HTML5 APIs  Universal access:  Accessibility  Media independence  Support for all world languages: Ex: supports Ruby annotation CHARACTERISTICS OF HTML5

 Present problem of plugin  Cannot always be installed  Can be disabled or blocked (Ex: iPad does not ship with a Flash plugin)  Often blocked in controlled corporate environments  Users disable because of unwelcome advertising  A separate attack vector  Difficult to integrate with the rest of HTML document A PLUGIN – FREE PARADIGM

 Canvas (2D or 3D)  Channel messaging  Cross-document messaging  Geolocation  MathML  Microdata  Server-Sent Event  Scalable Vector Graphics (SVG)  WebSocket API and protocol  Web origin concept  Web Storage  Web SQL database  Web Workers  XMLHTTPRequest Level 2 HTML5 FEATURES

 DOCTYPE   Character set  NEW DOCTYE AND CHARACTER SET

Content TypeDescription EmbeddedImports other resources into the document, EX: audio, video, canvas, and iframe FlowElements used in the body of documents and applications, EX: form, h1, and small HeadingSection headers, EX: h1, h2, and hgroup InteractiveContent that users interact with, EX: audio or video controls, button, and textarea MetadataCommonly found in the head section— Set up the presentation or behavior of the rest of the document, EX: script, style, and title PhrasingText and text markup elements, EX: mark, kbd, sub, and sup SectioningElements that define sections in the document, EX: article, aside, and title NEW AND DEPRECATED ELEMENTS Deprecated Elements: Elements perform inline styling in favor of using CSS, Ex: big, center, font

 The sectioning content type is useful for search engine.  New sectioning HTML5 elements SEMANTIC MARKUP Sectioning Element Description headerHeader content (for a page or a section of the page) footerFooter content (for a page or a section of the page) sectionA section in a web page articleIndependent article content asideRelated content or pull quotes navNavigational aids

  Define the relationship between a document and an external resource  Placed at  Attributes HTML5 TAG - AttributeValueDescription hrefURLSpecifies the location of the linked document relAlternate/author/help/icon/licenc e/next/pingback/prefetch/prev/se arch/sidebar/stylesheet/tag Specifies the relationship between the current document and the linked document  Example:

 webkit and moz mean they are not W3C standard  webkit  Supported by Safari and Chrome  -webkit-box-shadow: 2px 2px 20px #888; (Shadow effect)  -webkit-transform: rotate(-45deg); (Rotate effect)  -webkit-transform: scale(0.5); (Change size)  -webkit-border-radius: 10px;  moz  Supported by Mozilla  -moz-box-shadow: 2px 2px 20px #888; (Shadow effect)  -moz-transform: rotate(-45deg); (Rotate effect) CSS – WEBKIT/MOZ Excerise: Listing 1-1, 1-2

SIMPLIFYING SECTION – SELECTORS API FunctionExample getElementById() getElementById("foo"); getElementsByName() getElementsByName("foo"); getElementsByTagName() getElementsByTagName("input"); FunctionExampleResult querySelector()querySelector("input.error")Return the first input field with a style class of “error” querySelectorAll()querySelectorAll("#results td")Return any table cells inside the element with id results HTML4 HTML5 Excerise: Listing 1-3 HTML5

 JSON  A relatively new and increasing popular way to represent data  Data is represented as objects  In older browsers, a JavaScript library ( is necessary. Parsing and serializing are not always fast enough  Newer browsers have a native implementation of JSON  DOM Level 3  Most browsers support standard APIs for events and elements, Internet Explorer differs.  IE9 will support DOM level 2 and 3 features. WINDOWS.JSON AND DOM LEVEL 3

CHAP 3. AUDIO/VIDEO API

 An audio or video file is really just a container file, similar to a ZIP archive file that contains a number of files.  Some of the popular video container formats include the following:  Audio Video Interleave (.avi)  Flash Video (.flv)  MPEG 4 (.mp4)  Matroska (.mkv)  Ogg (.ogv) VIDEO CONTAINER

 Audio and video coders/decoders (codecs) are algorithms used to encode and decode a particular audio or video stream so that they can be played back.  Some example audio codecs are the following:  AAC  MPEG-3  Ogg Vorbis  Example video codecs are the following:  H.264  VP8  Ogg Theora AUDIO AND VIDEO CODECS

 Streaming audio and video.  That is, there is currently no standard for bitrate switching in HTML5 video  Media is restricted by HTTP cross-origin resource sharing.  Full-screen video is not scriptable  Because it could be considered a security violation to let a scriptable element take over the full screen.  Accessibility for audio and video elements is not fully specified yet.  Work is underway on a specification called WebSRT for subtitle support based on the popular SRT format. RESTRICTIONS

 Browser support for HTML5 Video  Checking for Browser Support  JavaScript  var hasVideo = !!(document.createElement('video').canPlayType);  HTML Fallback content   Your browser does not support HTML5 video.  BROWSER SUPPORT BrowserDetails ChromeVersion 3.0 and greater FirefoxVersion 3.5 and greater Internet Explorer Not supported OperaVersion 10.5 and greater SafariVersion 3.2 and greater

 Audio   An audio clip from Johann Sebastian Bach.   Video  HTML USAGE

 Common control functions TAKING CONTROL FunctionsBehavior load()Loads the media file and prepares it for playback. Normally does not need to be called unless the element itself is dynamically created. Useful for loading in advance of actual playback. play()Loads (if necessary) and plays the media file. Plays from the beginning unless the media is already paused at another position. pause()Pauses playback if currently active. canPlayType(type)Tests to see whether the video element can play a hypothetical file of the given MIME type.

  Play   function toggleSound() {  var music = document.getElementById("clickSound");  var toggle = document.getElementById("toggle");  if (music.paused) { // 檢查是否已暫停  music.play();  toggle.innerHTML = "Pause";  } else {  music.pause();  toggle.innerHTML ="Play";  }  WORKING WITH AUDIO

CHAP 2. CANVAS API

 Dynamically generate and render graphics, charts, images and animations  SVG (Scalable Vector Graphics) vs. Canvas  Bitmap canvas  Images drawn on a canvas are final and not be resized  Objects drawn on a canvas are not part of the page’s DOM or any namespace  SVG images can be scaled seamlessly at different resolutions and allow hit detection  HTML5 Canvas API  It performs well because it does not have to store objects for every primitive it draws  Relatively easy to implement the Canvas API WHAT IS CANVAS

 Element:  Coordinates: CANVAS BrowserDetails ChromeSupported in version 1.0 and greater FirefoxSupported in version 1.5 and greater Internet Explorer Not supported OperaSupported in version 9.0 and greater SafariSupported in version 1.3 and greater

CHECKING BROWSER SUPPORT try { document.createElement("canvas").getContext("2d"); document.getElementById("support").innerHTML = "HTML5 Canvas is supported in your browser."; } catch (e) { document.getElementById("support").innerHTML = "HTML5 Canvas is not supported  in your browser."; }

 A basic canvas with solid border   Get the canvas element and its context var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d');  Draw line  Prepare graph context.beginPath(); context.moveTo(70, 140); context.lineTo(140, 70);  Stroke graph onto the canvas  context.stroke(); CANVAS, DRAW LINE Excerise

TRANSFORMATION (TRANSLATE, SCALE, ROTATE)

 Get the canvas element and its context  Save the current drawing state context.save();  Move to new coordinate, ex: (50, 100) context.translate(50, 100);  Draw line  Restore the old drawing state context.restore(); TRANSLATION Excerise

PATH context.beginPath(); context.moveTo(-25, -50); context.lineTo(-10, -80); context.lineTo(-20, -80); context.lineTo(-5, -110; context.lineTo(-15, -110); context.lineTo(0, -140); context.lineTo(15, -110); context.lineTo(5, -110); context.lineTo(20, -80); context.lineTo(10, -80); context.lineTo(25, -50); ontext.closePath();

 Line width  context.lineWidth = 4;  Corner style at path joins (round: 圓角, bevel: 斜角, miter)  context.lineJoin = 'round';  Line style at endpoints (round, square, butt: 預設值 )  Context.lineCap = ‘square';  Stroke style  Change color  context.strokeStyle = '#663300';  Background pattern  Fill Style  Change color  context.fillStyle = '#339900';  Background pattern  Fill the region inside all the closed paths  context.fill();  Fill rectangular content  context.fillRect(x, y, w, h); //ex: context.fillRect(-5, -50, 10, 50); STROKE STYLE

 Starting Point: current location  context.quadraticCurveTo(ControlPointX, ControlPointY, EndPointX, EndPointY);  Example:  context.save();  context.translate(-10, 350);  cucontext.moveTo(0, 0);  context.quadraticCurveTo(170, -50, 260, -190);  context.quadraticCurveTo(310, -250, 410, -250);  context.lineWidth = 20;  context.strokeStyle = '#663300';  ontext.stroke();  context.restore(); QUADRATIC CURVE

 Load image var img = new Image(); img.src = “bark.jpg”;  Confirm the image is loaded img.onload = function(){ //Draw image onto canvas }  Draw image onto canvas  context.drawImage(image, dx, dy)  context.drawImage(image, dx, dy, dw, dh)  context.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)  Example  var bark = new Image();  bark.src = "bark.jpg";  bark.onload = function(){  context.drawImage(bark, -5, -50, 10, 50);  context.stroke();  context.restore();  } IMAGE

 Linear Gradient ( 漸層 )  Usage  context.createLinearGradient(x0, y0, x1, y1)  x0, y0 - Line start  x1, y1 - Line end  gradient.addColorStop(offset, color)  offset - From 0.0 to 1.0  color - Use rgba() or HEX  Example  var trunkGradient = context.createLinearGradient(-5, -50, 5, -50);  trunkGradient.addColorStop(0, '#663300');  trunkGradient.addColorStop(0.4, '#996600');  trunkGradient.addColorStop(1, '#552200');  context.fillStyle = trunkGradient;  context.fillRect(-5, -50, 10, 50);  Radical Gradient  Usage  Context.createRadicalGradient(x0, y0, r0, x1, y1, r1)  x0, y0, r0 – First circle center at (x0, y0) with radius r0  x1, y1, r1 – Second circle center at (x1, y1) with radius r1 GRADIENT

 Usage  context.createPattern(image, repeat)  repeat - repeat, repeat-x, repeat-y, no-repeat  Example  var gravel = new Image();  gravel.src = "gravel.jpg";  context.save();  context.translate(-10, 390);  gravel.onload = function(){  context.beginPath();  context.moveTo(0, 0);  context.quadraticCurveTo(170, -50, 260, -190);  context.quadraticCurveTo(310, -250, 410, -250);  context.lineWidth = 20;  context.strokeStyle = context.createPattern(gravel, 'repeat');  context.stroke();  context.restore();  } BACKGROUND PATTERN

 Usage  context.scale(rx, ry);  rx – width scale ratio  ry – height scale ratio  Example  var canvas = document.getElementById('canvas');  var context = canvas.getContext('2d');  context.save();  context.translate(260, 500);  context.scale(2, 2);  drawTree(context);  context.restore(); SCALE

 Usage  context.rotate(angle)  angel can be express with Math.PI/180 * degree  Example  var canvas = document.getElementById('canvas');  var context = canvas.getContext('2d');  context.save();  context.translate(260, 500);  context.rotate(Math.PI/180 * 40);  drawTree(context);  context.restore(); ROTATE

 Context.transform(rx, sy, sx, ry, dx, dy)  rx – width scale ratio  ry – height scale ratio  sy – vertical shear  sx – horizontal shear  Example  var canvas = document.getElementById('canvas');  var context = canvas.getContext('2d');  context.save();  context.translate(130, 250);  context.transform(1, 0, -0.5, 1, 0, 0);  context.scale(1, 0.6);  context.fillStyle = 'rgb(0, 0, 0, 0.2)';  context.fillRect(-5, -50, 10, 50);  createCanopyPath(context);  context.fill();  context.restore(); TRANSFORM

 Usage  context.fillText(text, x, y, maxwidth)  context.strokeText(text, x, y, maxwidth)  Property  context.font = Font String  context.textAlign = start, end, left, right, center  context.textBaseLine = top, middle, bottom, …  Example  context.save();  context.font = '60px 標楷體 ';  context.fillStyle = '#996600';  context.textAlign = 'center';  context.fillText(' 快樂圖畫 ', 200, 60, 400);  context.restore(); CANVAS TEXT

 Usage  shadowColor – Any CSS Color  shadowOffsetX – Pixel Count  shadowOffsetY – Pixel Count  Shadowblur – Gaussian blur  Example  context.shadowColor = 'rgba(0, 0, 0, 0.2)';  context.shadowOffsetX = 15;  context.shadowOffsetY = -10;  context.shadowBlur = 2;  context.font = '60px 標楷體 ';  context.fillStyle = '#996600';  context.textAlign = 'center';  context.fillText(' 快樂圖畫 ', 200, 60, 400); SHADOWS

CHAP 4. GEOLOCATION API

 You can request users to share their location and, if they agree, you can provide them with instructions on how to get to a nearby store to pick up a new pair of shoes at a discounted rate.  You request a position and, if the user agrees, the browser returns location information.  The position is provided to the browser by the underlying device (for example, a laptop or a mobile phone) on which the HTML5 Geolocation–enabled browser is running.  IP Address  Global Positioning System (GPS)  Wi-Fi with MAC addresses from RFID, Wi-Fi, and Bluetooth  GSM or CDMA cell phone IDs ABOUT LOCATION INFORMATION

ProsCons GPS Very accurate Take a long time, can drain a user’s device’s batteries Does not work well indoors May require additional hardware Wi-Fi Accurate Works indoors Fix quickly & cheaply Not good in rural areas with few wireless access points Cell Phone Fairly accurate Works indoors Fix quickly & cheaply Requires a device with access to a cell phone or cell modem Not good in rural areas with fewer cell phone towers User-Defined May have more accurate location data Allows services for alternate locations User entry might be faster than detection Can also be very inaccurate, especially if the location changes THE PROS AND CONS OF GEOLOCATION DATA

 The browser intercepts and requests user permission at step 2. ARACHITECTURE & PRIVACY

 Browser support for HTML5 Geolocation BROWSER SUPPORT BrowserDetails ChromeSupported in Google Chrome version 2 with Gears FirefoxSupported in version 3.5 and greater Internet ExplorerSupported via the Gears plugin OperaPlanned support version 10, experimental support in nightly builds SafariSupported in version 4 for the iPhone

 if(navigator.geolocation) {  document.getElementById("support").innerHTML = "HTML5 Geolocation supported.";  } else {  document.getElementById("support").innerHTML = "HTML5 Geolocation is not supported in your browser.";  } CHECKING FOR BROWSER SUPPORT

 Theres are two type of position requests  One-shot position request  Repeated position updates POSITIONS REQUESTS

 Usage  void getCurrentPosition(in PositionCallback successCallback,  in optional PositionErrorCallback errorCallback,  in optional PositionOptions options);  Example  navigator.geolocation.getCurrentPosition(updateLocation, handleLocationError, {timeout:10000});  updateLocation is a PositionCallback function will implement later  handleLocationError is a PositionErrorCallback function will implement later ONE-SHOT POSITION REQUESTS

 function updateLocation(position) {  var latitude = position.coords.latitude;  var longitude = position.coords.longitude;  var accuracy = position.coords.accuracy;  document.getElementById("latitude").innerHTML = latitude;  document.getElementById("longitude").innerHTML = longitude;  document.getElementById(“accuracy”).innerHTML = “This location is accurate within “ +accuracy + “ meters.”  } IMPLEMENT POSITION CALLBACK

 function handleLocationError(error) {  switch(error.code) {  case 0:  updateStatus(“Retrieving location error: " + error.message);  break;  case 1:  updateStatus("The user prevented retrieving location.");  break;  case 2:  updateStatus(“Unable to determine your location: " + error.message);  break;  case 3:  updateStatus(“Retrieving timed.");  break;  } IMPLEMENT POSITION ERROR CALLBACK

 Usage  long watchPosition(in PositionCallback successCallback,  in optional PositionErrorCallback errorCallback,  in optional PositionOptions options);  Example  navigator.geolocation.watchPosition(updateLocation, handleLocationError);  updateLocation is a PositionCallback function same as getCurrentPosition implemented above.  handleLocationError is a PositionErrorCallback function same as getCurrentPosition implemented above. REPEAT POSITION UPDATES

 Usage  navigator.geolocation.clearWatch(watchId);  Example  var watchId = navigator.geolocation.watchPosition(updateLocation, handleLocationError);  navigator.geolocation.clearWatch(watchId); CANCEL POSITION UPDATES

 Require Google Map JavaScript API   Put Div for Google Map  SHOW ME ON A GOOGLE MAP

 Google Map Example  var map; function initialize() { map = new google.maps.Map(document.getElementById('map_canvas'), {zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP}); navigator.geolocation.getCurrentPosition(function(position) { var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); var infowindow = new google.maps.InfoWindow({ map: map, position: pos, content: 'Location found using HTML5.‘ }); map.setCenter(pos); }, function(error) { alert('Cannot get Location data: ' + error.code); }); } window.addEventListener("load", initialize, true); SHOW ME ON A GOOGLE MAP (CONT.)

CHAP 5. COMMUNICATION API

 Communications between frames, tabs, and windows in a running browser was entirely restricted due to security concerns.  If browsers granted the ability to programmatically access the content loaded into other frames and tabs, sites would be able to steal whatever information they could get from another site's content using scripting CROSS DOCUMENT SECURITY ISSUE

 However, there are some legitimate cases for content from different sites to be able to communicate inside the browser.  The classic example is the "mashup", a combination of different applications such as mapping, chat, and news from different sites, all combined together to form a new meta- application.  To meet this need, the browser vendors and standards bodies agreed to introduce a new feature: Cross Document Messaging. CROSS DOCUMENT MESSAGING

CROSS DOCUMENT MESSAGING (CONT.)

 HTML5 clarifies and refines domain security by introducing the concept of an origin.  An origin is a subset of an address used for modeling trust relationships on the Web.  Origins are made up of a scheme, a host, and a port.  Cross-origin communication identifies the sender by origin. This allows the receiver to ignore messages from origins it does not trust or does not expect to receive messages from. UNDERSTANDING ORIGIN SECURITY

 Browser support for HTML5 Cross Document Messaging  Checking for Browser Support  if (typeof window.postMessage === “undefined”) { // postMessage not supported in this browser } BROWSER SUPPORT BrowserDetails ChromeSupported in version 2.0 and greater FirefoxSupported in version 3.0 and greater Internet ExplorerSupported in version 8.0 and greater OperaSupported in version 9.6 and greater SafariSupported in version 4.0 and greater

 Usage  window.postMessage(“Hello, world”, “portal.example.com”);  Example  document.getElementsByTagName(“iframe”)[0].contentWindow.post Message(“Hello, world”, “chat.example.net”); SENDING MESSAGES

 Example:  function messageHandler(e) { if(checkWhiteList(e.origin)) { processMessage(e.data); } else { // ignore messages from unrecognized origins } } window.addEventListener(“message”, messageHandler, true);  checkWhiteList is a function to check e.origin whether is legal. LISTEN FOR MESSAGE EVENTS

 XMLHttpRequest is the API that made Ajax possible.  XMLHttpRequest Level 2—the new version of XMLHttpRequest—has been significantly enhanced.  Cross-origin XMLHttpRequests  Progress events XML HTTPREQUEST LEVEL 2

CROSS-ORIGIN XMLHTTPREQUEST

 XMLHttpRequest Level 2 allows for cross-origin XMLHttpRequests using Cross Origin Resource Sharing (CORS)  CORS uses the origin concept discussed in the earlier Cross Document Messaging section.  Cross-origin HTTP requests have an Origin header. This header provides the server with the request’s origin.  This means that successful communication may require a CORS-capable server. CROSS-ORIGIN XMLHTTPREQUEST (CONT.)

 Access-Control-Allow-Origin:  Access-Control-Allow-Credentials: true CORS HEADER IN HTTP PROTOCOL

 In the previous version of XMLHttpRequest, there was only a single readystatechange event.  The readyState change event lacked a way to communicate upload progress.  Implementing an upload progress bar was not a trivial task and involved server-side participation.  XMLHttpRequest Level 2 introduces progress events with meaningful names. PROGRESS EVENT Progress Event Name loadstart progress abort error load loadend

 Browser support for HTML5 XMLHttpRequest BROWSER SUPPORT BrowserDetails ChromeSupported in version 2.0 and greater FirefoxSupported in version 3.5 and greater Internet ExplorerNot supported OperaNot supported SafariSupported in version 4.0 and greater

 var xhr = new XMLHttpRequest(); if (typeof xhr.withCredentials === undefined) { // Your browser does not support cross-origin XMLHttpRequest"; } else { // Your browser does support cross-origin } CHECKING FOR BROWSER SUPPORT

 Example  var crossOriginRequest = new XMLHttpRequest();  crossOriginRequest.open("GET", " true);  Make sure, you listen for errors. There are many reasons why this request might not succeed. For example, network failure, access denied, and lack of CORS support on the target server MAKING CROSS-ORIGIN REQUESTS

 Example  crossOriginRequest.onprogress = function(e) { var total = e.total; var loaded = e.loaded; if (e.lengthComputable) { // do something with the progress information } } USING PROGRESS EVENTS

 Example:  var xhr = new XMLHttpRequest();  var targetLocation = “  Document.getElementById(“sendButton”).addEventListener(“click”, function() {  xhr.upload.onprogress = function(e) {  Var ratio = e.loaded / e.total;  setProgress(ratio + “% uploaded”);  }  xhr.onprogress = function(e) {  Var ratio = e.loaded / e.total;  setProgress(ratio + “% downloaded”);  }  xhr.onload = function(e) {  setProgress(“finished”);  }  xhr.onerror = function(e) {  setProgress(“error”);  }  xhr.open(“POST”, targetLocation, true);  var filesToBeUploaded = document.getElementById("fileUpload");  var file = filesToBeUploaded.files[0]; // 取得欲上傳的檔案  xhr.send(file);  }, true); USING PROGRESS EVENTS

CHAP 5. WEBSOCKET API

 In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page.  But that’s obviously not a great solution.  With polling, the browser sends HTTP requests at regular intervals and immediately receives a response.  Making unnecessary requests inevitable and as a result, many connections are opened and closed needlessly in low-message-rate situations.  With long-polling, the browser sends a request to the server and the server keeps the request open for a set period of time.  When you have a high message-volume, long-polling does not provide any substantial performance improvements over traditional polling. REAL-TIME AND HTTP

 With streaming, the browser sends a complete request, but the server sends and maintains an open response that is continuously updated and kept open indefinitely (or for a set period of time).  Streaming is still encapsulated in HTTP, intervening firewalls and proxy servers may choose to buffer the response  increasing the latency of the message delivery.  All of these methods for providing real-time data involve HTTP request and response headers, which contain lots of additional, unnecessary header data and introduce latency REAL-TIME AND HTTP

THE WEBSOCKET HANDSHAKE

A DRAMATIC REDUCTION IN UNNECESSARY NETWORK TRAFFIC AND LATENCY

LATENCY COMPARISION

 Browser support for HTML5 WebSocket  Checking for Browser Support  if (window.WebSocket) { // support Websocket } else { // not support } BROWSER SUPPORT BrowserDetails ChromeSupported in version 4+ FirefoxSupported in version 4+ Internet ExplorerNot supported yet OperaNot supported yet SafariSupported in version 5+

 Creating a WebSocket  url = "ws://localhost:8080/echo";  w = new WebSocket(url);  Add Event Listeners  w.onopen = function() { log("open"); w.send("thank you for accepting this websocket request"); }  w.onmessage = function(e) { log(e.data); }  w.onclose = function(e) { log("closed"); }  Sending Message  document.getElementById("sendButton").onclick = function() { w.send(document.getElementById("inputMessage").value); } BASIC API USAGE

 A Simple Java WebSocket Server   Compile WebSocket Server Example  javac -cp dist/WebSocket.jar example/ChatServer.java  Run WebSocket Server  java -cp dist/WebSocket.jar;example ChatServer WEBSOCKET SERVER

 function connectServer() {  var url = "ws://localhost:8887";  socket = new WebSocket(url);  socket.onopen = function() {  updateSocketStatus("Connected to WebSocket server");  }  socket.onmessage = function(e) {  updateSocketStatus("Update message: " + e.data);  }  function sendMessage() {  var message = document.getElementById("sendMessage").value;  socket.send(message);  }  document.getElementById("connectButton").addEventListener("click", connectServer);  document.getElementById("sendMsgButton").addEventListener("click", sendMessage); A WEBSOCKET CHATROOM