JavaScript APIsHTML5
Remy Sharp@rem
standards.next
JavaScript APIsHTML5
Remy Sharp@rem
standards.next
& friends!
HTML5: 2022?
Bollocks.
APIs
•Canvas
•Drag & Drop
•History
•Inline Editing
•Messaging
•Of!ine Apps
•Video & Audio
•Geolocation
•Local Storage
•Selectors
•Server Events
•Web Sockets
•Workers
APIs
•Canvas
•Drag & Drop
•History
•Inline Editing
•Messaging
•Of!ine Apps
•Video & Audio
•Geolocation
•Local Storage
•Selectors
•Server Events
•Web Sockets
•Workers
APIs
•Canvas
•Drag & Drop
•History
•Inline Editing
•Messaging
•Of!ine Apps
•Video & Audio
•Geolocation
•Local Storage
•Selectors
•Server Events
•Web Sockets
•Workers
APIs
•Canvas
•Drag & Drop
•History
•Inline Editing
•Messaging
•Of!ine Apps
•Video & Audio
•Geolocation
•Local Storage
•Selectors
•Server Events
•Web Sockets
•Workers
APIs
•Canvas
•Drag & Drop
•History
•Inline Editing
•Messaging
•Of!ine Apps
•Video & Audio
•Geolocation
•Local Storage
•Selectors
•Server Events
•Web Sockets
•Workers
?
Documentation
www.whatwg.org/html5/
dev.w3.org/html5/
irc://irc.freenode.net/#whatwg
Canvas
Canvas
document.querySelector('canvas').getContext("2d")
http://tr.im/pRkz
document.querySelector('canvas').getContext("2d")
http://tr.im/pRkz
Drag'n Drop
Drag'n Drop
Drag & Drop
•draggable="true"
•events: dragstart, drop, etc
•event.transferData
<div draggable="true">drag me</div>
<script>
document.querySelector('div').addEventListener(
"dragstart",
function (e) {
e.dataTransfer.setData("arbitrary","data");
return true;
},
true);
</script>
<div draggable="true">drag me</div>
<script>
document.querySelector('div').addEventListener(
"dragstart",
function (e) {
e.dataTransfer.setData("arbitrary","data");
return true;
},
true);
</script>
???
el.addEventListener('dragover', function (e) {
e.preventDefault();
}, true);
el.addEventListener('drop', function (e) {
e.stopPropagation();
alert(e.dataTransfer.getData('arbitrary'));
}, true);
el.addEventListener('dragover', function (e) {
e.preventDefault();
}, true);
el.addEventListener('drop', function (e) {
e.stopPropagation();
alert(e.dataTransfer.getData('arbitrary'));
}, true);
el.addEventListener('dragover', function (e) {
e.preventDefault();
}, true);
el.addEventListener('drop', function (e) {
e.stopPropagation();
alert(e.dataTransfer.getData('arbitrary'));
}, true);
Of!ine Applications
Of!ine Applications
Of!ine Apps
•Application cache
•Events: of!ine, online
•navigator.onLine property
Enable
<html manifest="my.manifest">
CACHE MANIFEST
images/shade.jpg
images/bin.jpg
my.manifest
Cache
•First line: CACHE MANIFEST
•Requires text/cache-manifest
•Recommend using versioning
•window.applicationCache
Cache
•On load will hit my.manifest
Cache
•On load will hit my.manifest
•Change manifest: trigger reload
Cache
•On load will hit my.manifest
•Change manifest: trigger reload
•applicationCache.update() force
Cache
•On load will hit my.manifest
•Change manifest: trigger reload
•applicationCache.update() force
•Cache events
Firefox
window.addEventListener( 'offline', // online too online, // function true);
function online() {
if (navigator.onLine == false) {
// gone offline
} else {
// else we're online
}
}
navigator.onLine
Geolocation
Geolocation
Not always accurate!
navigator .geolocation .getCurrentPosition( success, err );
Messaging
Messaging
Messaging
•Communicate across domains
•Across window object
•With Workers
•String transfer only
.postMessage(str)
.onMessage(event)event.data == str
Cross Domain
document .getElementById("iframe") .contentWindow .postMessage("my message");
The Catcher
window.addEventListener( "message", function(e){ if (e.origin !== "http://example.com") { return; }
alert(e.origin + " said: " + e.data); }, false);
Web Workers
Web Workers
•Threads
•Threads
•Native or via Gears
•Threads
•Native or via Gears
•Sandboxed
•Threads
•Native or via Gears
•Sandboxed
•Debugging?
•importScripts
•postMessage
•onmessage
•onconnect
Without
Storage
Storage
1. sessionStorage
1. sessionStorage
2. localStorage
1. sessionStorage
2. localStorage
3. database storage
Storage
sessionStorage.setItem(key, value)
sessionStorage.getItem(key)
Storage
localStorage.setItem(key, value)
localStorage.getItem(key)