日期:2014-05-17 浏览次数:20730 次
HTML5 has a lot of new features. There’s new HTML, CSS and, of course, JavaScript. Officially HTML5 specification and implementation won’t be ready until 2022. Personally I don’t believe in this. Majority of JavaScript features?described further are already implemented in modern browsers (e.g. Sarafi, Chrome, Firefox, Opera). Even Internet Explorer gained capability to render canvas and other stuff (supposing we use ExplorerCanvas or Chrome Frame ). Take a closer look at what’s going to make your live better and happier :
?
How many times have you wondered why there’s getElementById , getElementsByTagName , but there is not getElementByClassName ? New JavaScript API solves this issue:
var elements = document.getElementsByClassName('entry');
Moreover there’s now possibility to fetch elements that match provided CSS syntax!
var elements = document.querySelectorAll("ul li:nth-child(odd)");
var first_td = document.querySelector("table.test > tr > td");
Cookie mechanism has some disadvantages. As W3C said :
Well, sessionStorage has been created to let developers cope with first of above troubles. It keeps data in per tab storage. To get along with second one, W3C has introduced localStorage – the persistent storage that never expires.
Look, how simple is saving draft every new character is pressed:
textarea.addEventListener('keyup', function () {
window.localStorage['value'] = area.value;
window.localStorage['timestamp'] = (new Date()).getTime();
}, false);
textarea.value = window.localStorage['value'];
Go to demo
What about database accessible directly from JavaScript? Since now we no longer parse, sort, filter data using ?consuming-lot-of-memory-and-cpu JavaScript loops. We fetch data using well know SQL queries. Look at example:
var db = window.openDatabase("Database Name", "Database Version");
db.transaction(function(tx) {
tx.executeSql("SELECT * FROM test", [], successCallback, errorCallback);
});
Database is stored on client’s computer so it’s secure.
Go to demo
Web SQL Storage is available even if client went offline. But if we want create fully-functional offline aplication, we must care about resources like images, CSS, JS and et caetera. It’s high time to familiarize with Application Cache API.
We create cache.manifest file and link to it from html element.
<html
manifest="cache.manifest"
>
File must be served with text/cache-manifest mimetype and contain body like this:
CACHE MANIFEST
CACHE:
index.html
help.html
style
/default.css
images/logo.png