html5 js apis

76
JavaScript APIs HTML5 Remy Sharp @rem standards.next

Upload: remy-sharp

Post on 08-Sep-2014

106.315 views

Category:

Technology


2 download

DESCRIPTION

Standards.next for HTML5 JavaScript APIs

TRANSCRIPT

Page 1: HTML5 JS APIs

JavaScript APIsHTML5

Remy Sharp@rem

standards.next

Page 2: HTML5 JS APIs

JavaScript APIsHTML5

Remy Sharp@rem

standards.next

& friends!

Page 3: HTML5 JS APIs

HTML5: 2022?

Page 4: HTML5 JS APIs

Bollocks.

Page 5: HTML5 JS APIs

APIs

•Canvas

•Drag & Drop

•History

•Inline Editing

•Messaging

•Of!ine Apps

•Video & Audio

•Geolocation

•Local Storage

•Selectors

•Server Events

•Web Sockets

•Workers

Page 6: HTML5 JS APIs

APIs

•Canvas

•Drag & Drop

•History

•Inline Editing

•Messaging

•Of!ine Apps

•Video & Audio

•Geolocation

•Local Storage

•Selectors

•Server Events

•Web Sockets

•Workers

Page 7: HTML5 JS APIs

APIs

•Canvas

•Drag & Drop

•History

•Inline Editing

•Messaging

•Of!ine Apps

•Video & Audio

•Geolocation

•Local Storage

•Selectors

•Server Events

•Web Sockets

•Workers

Page 8: HTML5 JS APIs

APIs

•Canvas

•Drag & Drop

•History

•Inline Editing

•Messaging

•Of!ine Apps

•Video & Audio

•Geolocation

•Local Storage

•Selectors

•Server Events

•Web Sockets

•Workers

Page 9: HTML5 JS APIs

APIs

•Canvas

•Drag & Drop

•History

•Inline Editing

•Messaging

•Of!ine Apps

•Video & Audio

•Geolocation

•Local Storage

•Selectors

•Server Events

•Web Sockets

•Workers

?

Page 10: HTML5 JS APIs

Documentation

Page 11: HTML5 JS APIs

www.whatwg.org/html5/

dev.w3.org/html5/

irc://irc.freenode.net/#whatwg

Page 12: HTML5 JS APIs
Page 13: HTML5 JS APIs
Page 14: HTML5 JS APIs
Page 15: HTML5 JS APIs

Canvas

Page 16: HTML5 JS APIs

Canvas

Page 17: HTML5 JS APIs

document.querySelector('canvas').getContext("2d")

http://tr.im/pRkz

Page 18: HTML5 JS APIs

document.querySelector('canvas').getContext("2d")

http://tr.im/pRkz

Page 19: HTML5 JS APIs

Drag'n Drop

Page 20: HTML5 JS APIs

Drag'n Drop

Page 21: HTML5 JS APIs

Drag & Drop

•draggable="true"

•events: dragstart, drop, etc

•event.transferData

Page 22: HTML5 JS APIs

<div draggable="true">drag me</div>

<script>

document.querySelector('div').addEventListener(

"dragstart",

function (e) {

e.dataTransfer.setData("arbitrary","data");

return true;

},

true);

</script>

Page 23: HTML5 JS APIs

<div draggable="true">drag me</div>

<script>

document.querySelector('div').addEventListener(

"dragstart",

function (e) {

e.dataTransfer.setData("arbitrary","data");

return true;

},

true);

</script>

???

Page 24: HTML5 JS APIs

el.addEventListener('dragover', function (e) {

e.preventDefault();

}, true);

el.addEventListener('drop', function (e) {

e.stopPropagation();

alert(e.dataTransfer.getData('arbitrary'));

}, true);

Page 25: HTML5 JS APIs

el.addEventListener('dragover', function (e) {

e.preventDefault();

}, true);

el.addEventListener('drop', function (e) {

e.stopPropagation();

alert(e.dataTransfer.getData('arbitrary'));

}, true);

Page 26: HTML5 JS APIs

el.addEventListener('dragover', function (e) {

e.preventDefault();

}, true);

el.addEventListener('drop', function (e) {

e.stopPropagation();

alert(e.dataTransfer.getData('arbitrary'));

}, true);

Page 27: HTML5 JS APIs

http://html5demos.com/drag

Page 28: HTML5 JS APIs

http://html5demos.com/drag

Page 29: HTML5 JS APIs

Of!ine Applications

Page 30: HTML5 JS APIs

Of!ine Applications

Page 31: HTML5 JS APIs

Of!ine Apps

•Application cache

•Events: of!ine, online

•navigator.onLine property

Page 32: HTML5 JS APIs

Enable

<html manifest="my.manifest">

Page 33: HTML5 JS APIs

CACHE MANIFEST

images/shade.jpg

images/bin.jpg

my.manifest

Page 34: HTML5 JS APIs

Cache

•First line: CACHE MANIFEST

•Requires text/cache-manifest

•Recommend using versioning

•window.applicationCache

Page 35: HTML5 JS APIs

Cache

•On load will hit my.manifest

Page 36: HTML5 JS APIs

Cache

•On load will hit my.manifest

•Change manifest: trigger reload

Page 37: HTML5 JS APIs

Cache

•On load will hit my.manifest

•Change manifest: trigger reload

•applicationCache.update() force

Page 38: HTML5 JS APIs

Cache

•On load will hit my.manifest

•Change manifest: trigger reload

•applicationCache.update() force

•Cache events

Page 39: HTML5 JS APIs
Page 40: HTML5 JS APIs
Page 41: HTML5 JS APIs

Firefox

Page 42: HTML5 JS APIs

window.addEventListener( 'offline', // online too online, // function true);

Page 43: HTML5 JS APIs

function online() {

if (navigator.onLine == false) {

// gone offline

} else {

// else we're online

}

}

Page 44: HTML5 JS APIs

http://html5demos.com/offline

Page 45: HTML5 JS APIs

http://html5demos.com/offline

Page 46: HTML5 JS APIs

navigator.onLine

Page 47: HTML5 JS APIs

Geolocation

Page 48: HTML5 JS APIs

Geolocation

Page 49: HTML5 JS APIs

Not always accurate!

Page 50: HTML5 JS APIs
Page 51: HTML5 JS APIs

navigator .geolocation .getCurrentPosition( success, err );

Page 52: HTML5 JS APIs
Page 53: HTML5 JS APIs
Page 54: HTML5 JS APIs

Messaging

Page 55: HTML5 JS APIs

Messaging

Page 56: HTML5 JS APIs

Messaging

•Communicate across domains

•Across window object

•With Workers

•String transfer only

Page 57: HTML5 JS APIs

.postMessage(str)

.onMessage(event)event.data == str

Page 58: HTML5 JS APIs

Cross Domain

document .getElementById("iframe") .contentWindow .postMessage("my message");

Page 59: HTML5 JS APIs

The Catcher

window.addEventListener( "message", function(e){ if (e.origin !== "http://example.com") { return; }

alert(e.origin + " said: " + e.data); }, false);

Page 60: HTML5 JS APIs

Web Workers

Page 61: HTML5 JS APIs

Web Workers

Page 62: HTML5 JS APIs

•Threads

Page 63: HTML5 JS APIs

•Threads

•Native or via Gears

Page 64: HTML5 JS APIs

•Threads

•Native or via Gears

•Sandboxed

Page 65: HTML5 JS APIs

•Threads

•Native or via Gears

•Sandboxed

•Debugging?

Page 66: HTML5 JS APIs

•importScripts

•postMessage

•onmessage

•onconnect

Page 67: HTML5 JS APIs

Without

Page 68: HTML5 JS APIs

http://html5demos.com/worker

Page 69: HTML5 JS APIs

Storage

Page 70: HTML5 JS APIs

Storage

Page 71: HTML5 JS APIs

1. sessionStorage

Page 72: HTML5 JS APIs

1. sessionStorage

2. localStorage

Page 73: HTML5 JS APIs

1. sessionStorage

2. localStorage

3. database storage

Page 74: HTML5 JS APIs

Storage

sessionStorage.setItem(key, value)

sessionStorage.getItem(key)

Page 75: HTML5 JS APIs

Storage

localStorage.setItem(key, value)

localStorage.getItem(key)