300 likes | 1.11k Views
PLAT-384P. Anatomy of HTML5 sites and Metro style apps using HTML5. Tony Ross Program Manager Microsoft Corporation. Agenda. What can I do with HTML5? Changing content on-the-fly Fetching data behind-the-scenes Keeping your code in-check. What can I do with HTML5?.
E N D
PLAT-384P Anatomy of HTML5 sites and Metro style apps using HTML5 Tony Ross Program Manager Microsoft Corporation
Agenda • What can I do with HTML5? • Changing content on-the-fly • Fetching data behind-the-scenes • Keeping your code in-check
What can I do with HTML5? • Play media (<audio> and <video>) • Build complex and attractive interfaces (CSS3) • Manipulate pixels (<canvas>) • Scale across devices (SVG) • Load and save files (File API) • Incorporate location awareness (Geolocation API) • And much more…
demo HTML5 Example Tony Ross Program Manager Internet Explorer
Basic StructureHTML <!DOCTYPE html> • <!DOCTYPEhtml> • <html> • <head> • <title>My App</title> • <linkrel="stylesheet"href="my.css"> • <linkrel="stylesheet"href="more.css"> • ... • </head> • <body> • ... content ... • <scriptsrc="my.js"></script> • <scriptsrc="more.js"></script> • </body> • </html> <html> <head> <link rel="stylesheet" href="my.css"> <link rel="stylesheet" href="more.css"> </head> <body> <script src="my.js"></script> <script src="more.js"></script> </body> </html>
Basic StructureCSS button • button{ • color: #000; • padding: 4px; • } • .page{ • background-color: #fff; • } • #my { • font-weight: bold; • } color: #000; padding: 4px; .page #my
Basic StructureJavaScript • // variables and objects • vara = {first : 1, second : 2}; • // functions • function add(x, y) { • returnx + y; • } • // function calls • a.result = add(a.first, a.second); {first : 1, second : 2} var a = function add (x, y) return a.result = add(a.first, a.second)
Locating ElementsUsing “querySelector” and “querySelectorAll” • HTML • <aclass="tab"value="1">...</a> • <aclass="tab"value="2">...</a> • <aclass="tab"value="3">...</a> • CSS • .tab { • border-radius: 5px5px00; • } • JavaScript • var tabs = document.querySelectorAll(".tab"); • for(vari = 0; i < tabs.length; i++) { • tabs[i].onclick = showTab; • } class="tab" class="tab" class="tab" .tab document.querySelectorAll(".tab"); tabs[i].onclick = showTab;
Listening to EventsUsing “on*” and “addEventListener” • JavaScript • // Simple registration • tabs[i].onclick = showTab; • // Alternate registration • tabs[i].addEventListener("click", showTab, false); onclick = showTab addEventListener("click", showTab, false)
Changing TextUsing “textContent” • HTML • <h2id="title">Section 1</h2> • CSS • #title { • font-size: 20px; • } • JavaScript • var title = document.querySelector("#title"); • functionsetTitle(name) { • title.textContent = name; • } id="title" #title var title = document.querySelector("#title"); title.textContent = name;
Adding ElementsUsing “createElement” and “appendChild” • HTML • <body> • <imgsrc="my.png"/> • </body> • JavaScript • functionaddImage() { • varimg = document.createElement("img"); • img.src = "my.png"; • document.body.appendChild(img); • } document.createElement("img"); document.body.appendChild(img);
Hiding ElementsUsing “display” • HTML • <sectionid="p1"class="page">...</section> • <sectionid="p2"class="page">...</section> • CSS • .page { • font-size: 20px; • } • JavaScript • function swap(oldPage, newPage) { • oldPage.style.display = "none"; • newPage.style.display = ""; • } oldPage.style.display = "none"; newPage.style.display = "";
demo Changing Content
Sending and Receiving DataUsing “XMLHttpRequest” • JavaScript • function load(url, data, callback) { • varxhr = newXMLHttpRequest(); • xhr.open("GET", url, true); • xhr.onload= function() { • callback(xhr.responseText); • } • xhr.send(data); • } varxhr = new XMLHttpRequest(); xhr.open("GET", url, true); xhr.onload = callback(xhr.responseText); xhr.send(data);
Transmitting Complex ObjectsUsing “JSON.parse” and “JSON.stringify” • JavaScript • functionloadJSON(url, data, callback) { • varxhr = newXMLHttpRequest(); • xhr.open("GET", url, true); • xhr.onload = function() { • callback( JSON.parse(xhr.responseText) ); • } • xhr.send( JSON.stringify(data) ); • } • loadJSON("my.json", { id : 1 }, function(response) { • setTitle(response.title); • }); JSON.parse(xhr.responseText) JSON.stringify(data) response { id : 1 } response.title
demo Fetching Data
Isolating JavaScriptUsing the Module Pattern • JavaScript • (function(){ • var title = document.querySelector("#title"); • functionsetTitle(name) { • title.textContent = name; • } • setTitle("My Title"); • })(); (function(){ setTitle("My Title"); })();
Exposing Public APIsUsing the Module Pattern • JavaScript • var module = (function(){ • var title = document.querySelector("#title"); • functionsetTitle(name) { • title.textContent = name; • } • return { setTitle : setTitle }; • })(); • module.setTitle("Custom Title"); var module = return { setTitle : setTitle }; module.setTitle("Custom Title");
demo Module Pattern
Recap • Build Metro style apps using HTML5 • Keep your apps navigation-free • Use XMLHttpRequest and JSON between client and server • Organize JavaScript using the module pattern
Related sessions • PLAT-373C: Building real-time web apps with HTML5 WebSockets • PLAT-376T: Building offline access in Metro style apps and websites using HTML5 • PLAT-379P: Building responsive apps and sites with HTML5 web workers • PLAT-381T: Building beautiful and interactive apps with HTML5 & CSS3 • Plat-382T: What's new with HTML5, Javascript, and CSS3 • PLAT-386T: 50 performance tricks to make your Metro style apps and sites using HTML5 faster • PLAT-551P: Programming SVG and canvas graphics in a Metro style app based on HTML5 • PLAT-873T: Designing Metro style apps using CSS3
Further reading and documentation • JavaScript: The Language http://channel9.msdn.com/events/mix/mix11/HTM06 • More HTML5 Examples:http://ietestdrive.com/http://www.beautyoftheweb.com/
thank you Feedback and questions http://forums.dev.windows.com Session feedbackhttp://bldw.in/SessionFeedback
© 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.