Slide 1

Slide 1 text

Javascript APIs DallasPHP | JAKE SMITH | March 12, 2013 IN THE HTML5 UMBRELLA Tuesday, March 12, 13

Slide 2

Slide 2 text

June 28 - 29, 2013 TICKETS GO ON SALE NEXT WEEK! Tuesday, March 12, 13

Slide 3

Slide 3 text

jakefolio http://jakefolio.com [email protected] Jake Smith Tuesday, March 12, 13

Slide 4

Slide 4 text

Javascript DOM ACCESS Tuesday, March 12, 13

Slide 5

Slide 5 text

document.querySelectorAll('.menu li'); $('.menu li'); document.querySelectorAll('a[class^=”menu]'); Throws SYNTAX_ERR for invalid // jQuery version Tuesday, March 12, 13

Slide 6

Slide 6 text

var items = document.querySelectorAll('.menu li'); var items = $('.menu li'); var subItems = items[0].querySelectorAll('li'); var subItems = $('.menu li').find('li'); Tuesday, March 12, 13

Slide 7

Slide 7 text

// Find the latest tweet var latestTweet = document.querySelector('#latest-tweet'); // jQuery version: $('#latest-tweet').first(); for item in items { // Log the content of element console.log(item.innerHTML); // Output the style object console.log(item.style); } Tuesday, March 12, 13

Slide 8

Slide 8 text

StaticNodeList vs. LiveNodeList Tuesday, March 12, 13

Slide 9

Slide 9 text

Javascript Element ClassList Tuesday, March 12, 13

Slide 10

Slide 10 text

var widget = document.getElementById('widget'); // Adds if doesn't exist, remove if present widget.classList.toggle('primary'); // Add Class widget.classList.add('primary'); // Remove Class widget.classList.remove('primary'); Tuesday, March 12, 13

Slide 11

Slide 11 text

var widget = document.getElementById('widget'); // Check if element has Class widget.classList.contains('primary'); // Get First Class widget.classList[0]; widget.classList.item(0); // Built-in toString method "Classes " + widget.classList // => Classes widget primary Tuesday, March 12, 13

Slide 12

Slide 12 text

DOMNodeList !== Array Tuesday, March 12, 13

Slide 13

Slide 13 text

Javascript STORAGE Tuesday, March 12, 13

Slide 14

Slide 14 text

// Check if localStorage is available if (typeof window.localStorage == 'object' && window.localStorage != null) { var store = window.localStorage; // localStorage is available! } Tuesday, March 12, 13

Slide 15

Slide 15 text

var store = window.localStorage; try { store.setItem('cow', 'moo'); } catch(e) { // Out of storage space? if (e == QUOTA_EXCEEDED_ERR) { console.log('ERROR: Out of space!'); } } Tuesday, March 12, 13

Slide 16

Slide 16 text

var status = document.getElementById('status'); // Change status text status.innerText = store.getItem('cow'); status.innerText = store['cow']; status.innerText = store.cow; delete store['cow']; // Delete all of localstorage store.clear(); Tuesday, March 12, 13

Slide 17

Slide 17 text

LocalStorage Gotchas - HTTP vs. HTTPS localstorage - store seperately (including incognito for chrome) - No default expiration. - Only support strings as values - SUPPORT: All modern browsers and IE8+ - POLYFILL: Use lawnchair for storage. Tuesday, March 12, 13

Slide 18

Slide 18 text

Tuesday, March 12, 13

Slide 19

Slide 19 text

Javascript APP CACHE Tuesday, March 12, 13

Slide 20

Slide 20 text

... mime-type: text/cache-manifest Tuesday, March 12, 13

Slide 21

Slide 21 text

CACHE MANIFEST # 2010-06-18:v2 CACHE: /favicon.ico index.html stylesheet.css images/logo.png scripts/main.js # Resources that require the user to be online. NETWORK: login.php /myapi http://api.twitter.com # static.html will be served if main.py is inaccessible # offline.jpg will be served in place of all images in images/large/ # offline.html will be served in place of all other .html files FALLBACK: /main.py /static.html images/large/ images/offline.jpg *.html /offline.html http://www.html5rocks.com/en/tutorials/appcache/beginner/ Unique Identifier Tuesday, March 12, 13

Slide 22

Slide 22 text

var appCache = window.applicationCache; // Attempt to update the user's cache. appCache.update(); if (appCache.status == window.applicationCache.UPDATEREADY) { // The fetch was successful, swap in the new cache. appCache.swapCache(); } Tuesday, March 12, 13

Slide 23

Slide 23 text

‣cached ‣checking ‣downloading ‣error ‣noupdate ‣obsolete ‣progress ‣updateready App Cache Events cache deleted or 404/410 cache downloaded, ready to load var appCache = window.applicationCache; appCache.status; Tuesday, March 12, 13

Slide 24

Slide 24 text

Javascript GEOLOCATION Tuesday, March 12, 13

Slide 25

Slide 25 text

// Check if geolocation is supported if (navigator.geolocation) { // Geolocation Supported! } else { // Geolocation not supported } Tuesday, March 12, 13

Slide 26

Slide 26 text

geoloc.getCurrentPosition(function(pos) { position = [ pos.coords.latitude, pos.coords.longitude ]; }, errorHandler); var position; var geoloc = navigator.geolocation; ASYNC Tuesday, March 12, 13

Slide 27

Slide 27 text

geoloc.watchPosition(function(pos) { // Successfully changed position position = [ pos.coords.latitude, pos.coords.longitude ]; } errorHandler, { enableHighAccuracy: true, maximumAge: 30000, timeout: 27000 } ); var geoloc = navigator.geolocation; Heavy Battery Usage Cache Lifetime Tuesday, March 12, 13

Slide 28

Slide 28 text

Javascript HISTORY Tuesday, March 12, 13

Slide 29

Slide 29 text

history.go(-1); history.back(); history.forward(); history.length; Tuesday, March 12, 13

Slide 30

Slide 30 text

history.pushState( { id: 1, title: 'Hello World'}, 'Page Title', '/url/goes/here' ); Page Title Ignored Accessed through history.state Tuesday, March 12, 13

Slide 31

Slide 31 text

history.replaceState( { id: 1, title: 'Hello World'}, 'Page Title', '/url/goes/here' ); Replaces current URL in history Tuesday, March 12, 13

Slide 32

Slide 32 text

History State has changed window.addEventListener('onpopstate', function(e) { console.log(e.data); }); var currentPageState = history.state; Tuesday, March 12, 13

Slide 33

Slide 33 text

Javascript COMMUNICATION Tuesday, March 12, 13

Slide 34

Slide 34 text

var ws = new WebSocket('ws://html5rocks.websocket.org/echo'); // Show current Ready State console.log(ws.readyState); CONSTANTS: CONNECTING = 0 OPEN = 1 CLOSING = 2 CLOSED = 3 Tuesday, March 12, 13

Slide 35

Slide 35 text

// When the connection is open, send data to the server ws.onopen = function () { ws.send('Ping'); // Send the message 'Ping' to server }; // Log errors ws.onerror = function (error) { console.log('WebSocket Error ' + error); }; // Log messages from the server ws.onmessage = function (e) { console.log('Server: ' + e.data); }; Tuesday, March 12, 13

Slide 36

Slide 36 text

// Say hello ws.send('Hello World'); // Close the connection ws.close(); Tuesday, March 12, 13

Slide 37

Slide 37 text

if (window.WebSocket) { // Supported var ws = new WebSocket('ws://html5rocks.websocket.org/echo'); // Show current Ready State console.log(ws.readyState); // When the connection is open, send some data to the server ws.onopen = function () { ws.send('Ping'); // Send the message 'Ping' to the server }; // Log errors ws.onerror = function (error) { console.log('WebSocket Error ' + error); }; // Log messages from the server ws.onmessage = function (e) { console.log('Server: ' + e.data); }; // Say hello ws.send('Hello World'); // Close the connection ws.close(); } Tuesday, March 12, 13

Slide 38

Slide 38 text

Resources ‣ http://dev.opera.com/web ‣ https://developer.mozilla.org/en-US/web ‣ http://caniuse.com/ ‣ http://davidwalsh.name ‣ http://www.html5rocks.com/en/tutorials/ Tuesday, March 12, 13

Slide 39

Slide 39 text

Thanks for Listening! jakefolio http://jakefolio.com [email protected] Tuesday, March 12, 13