Javascript and HTML 5 API
Javascript and HTML 5 API
APIs
What is JavaScript
Interactivity
Animation
Gaming
Web-servers (Node.js)
JS Uses
DOM Manipulation
Form input validation
Changing element styles
Event listening
DOM Manipulation
x = document.getElementById(“demo”);
x.innerHTML = “Hello There!”;
Form input validation
if(isNaN(x)) {
x = document.getElementById(“demo”);
x.style.color = “#FF0033”;
Event listening
<script>
function doSomething() {
alert(“Something is done...”);
</script>
x = document.getElementById(“demo”);
x.addEventListener(“dragstart”, function(e) {
e.dataTransfer.setData(“arbitrary”, “data”);
return true;
}, true);
</script>
Drag and Drop
Online Demo:
http://html5demos.com/drag
Offline Applications
Application Cache
Events: offline, online
navigator.onLine property
How to enable it?
<html manifest=”app.manifest”>
app.manifest
CACHE MANIFEST
assets/images/background.png
assets/images/logo.png
server-side
CACHE MANIFEST
assets/images/background.png
assets/images/logo.png
VERSION 13
Cache events
if(navigator.onLine == false) {
// load data cached in LS or WebSQL
} else {
// use AJAX...
// then, cache retrieved data for next
offline cycle
}
Cache events
Online Demo:
http://html5demos.com/offline
Geolocation
Caveats:
navigator
.geolocation
.getCurrentPosition(successCallback, errorCallback);
Data Returned
Storage
sessionStorage
localStorage
databaseStorage
Session Storage
Example:
divsToHide = document.getElementByTagName(“div”);
for(i=0;i<divsToHide.length;i++) {
divsToHide[i].style.display = ‘none’;
}
With jQuery
$(“div”).hide();
:)
How does it work
Find some HTML
Do something to it
hide
$(“div”).hide(); ‘em!
jQuery Selectors
The “query” part of jQuery
$(“div”)
$(“.myClass”)
$(“#myID”)
$(“li:first”)
$(“tr:odd”)
jQuery Selectors
The “query” part of jQuery
$(“a[target=_blank]”)
$(“form[id^=step]”)
$(“#myId, .myClass, table”)
Common use example
$(“.title”).addClass(“redbox”);
$(“.error-msg”).show().addClass(“error”);
$(“.error-
msg”).show().addClass(“error”).fadeIn();
$(“.error-msg”).html(“<p>Incorrect entry!</p>”);
jQuery methods
Moving Elements:
$(function() {
$(document).ready(function() {
// same thing...
});
GET / SET values
GET POST
.attr(‘id’) .attr(‘id’, ‘foo’)
.html() .html(‘<p>hi</p>’)
.width() .width(60)
Events
Click:
$(“#mybutton”).click(function() {
Unbinding events:
$(“#mybutton”).unbind(“expand”);
DEMO
Q&A
What’s next?