首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > CSS >

HTML5 JavaScript API. What’s new

2012-10-29 
HTML5 JavaScript API. What’s new?HTML5 has a lot of new features. There’s new HTML, CSS and, of cou

HTML5 JavaScript API. What’s new?

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 :

?

New Selectors

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");
Web Storage

Cookie mechanism has some disadvantages. As W3C said:

    A user could be buying plane tickets in two different windows, using the same site. If the site used cookies to keep track of which ticket the user was buying, then as the user clicked from page to page in both windows, the ticket currently being purchased would “leak” from one window to the other, potentially causing the user to buy two tickets for the same flight without really noticing.Web applications may wish to store megabytes of user data, such as entire user-authored documents or a user’s mailbox, on the client side for performance reasons.?Again, cookies do not handle this case well, because they are transmitted with every request.

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

Web SQL Database Storage

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

Offline Application Cache API

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.

CACHE MANIFESTCACHE:index

In addition to the new Web Sockets API, there is also a new protocol (the “web socket protocol“) that the browser uses to communicate with servers. We also developed?pywebsocket, which can be used as an Apache extension module, or can even be run as standalone server.

———-

Personally, there’s long way for web sockets to be fully supported by browsers. At this time I would recommend NodeJS or APE Project.

Notifications

Google Chrome has introduced new way to show notifications. They are popping outside browser window and user could see them even if browser is minimalized. Before showing notifications you must ask user for permission to do so. Look at code below and try demo:

if (window.webkitNotifications.checkPermission() == 0) {  // you can pass any url as a parameter  window.webkitNotifications.createNotification(tweet.picture, tweet.title,      tweet.text).show();} else {  window.webkitNotifications.requestPermission();}

Show tweetsSet notification permissions

Drag and Drop

Everyone knows what is it. Thanks to HTML5 we are able to drag and drop any element into any element, without heavy JavaScript frameworks. There’s also possibility to drag and drop text/images/files from other windows and desktop.

Go to demo

Drag and drop anything

Geolocation

Probably anything special, but since now we may recognize user’s location on frontend as well as backend. Nowadays geolocation is not always accurate?and is supported only by Firefox and Safari Mobile.

if (navigator.geolocation) {  navigator.geolocation.getCurrentPosition(function(position) {    var lat = position.coords.latitude;    var lng = position.coords.longitude;    map.setCenter(new GLatLng(lat, lng), 13);    map.addOverlay(new GMarker(new GLatLng(lat, lng)));  });}

Go to demo

Audio and Video manipulation

We can easily embed audio or video on page and super-duper-easily manipulate them with JavaScript.

<audio src="sound.mp3" controls></audio>document.getElementById("audio").muted=false;<video src='movie.mp4' autoplay controls></video>document.getElementById("video").play();

For non-modern browsers you may include?html5media. You may also be intrested in HTML5 video player.

Go to audio demo
Go to video demo

Canvas

The best of all! We are able to actually draw in browser. Even second Mac OS if we have patience. Canvas is supported by any browser (even IE thanks to ExplorerCanvas). There’s huge set of canvas demos at ChromeExperiments.com. There’s CAKE (scenegraph library for the?canvas tag). We could write games,?physics engines, editors and even whole UI. Flash CS5 will export to HTML5 Canvas and even now we are able to run swf files using pure JavaScript and <canvas> tag (yes, now we can run flash on iPhone).

Useful links
    Check browser support for HTML5 features and view some demosCurrent HTML specificationHTML5 PresentationDetect support for HTML5 features with Modernizr

RT: http://blog.frontendforce.com/2010/04/html5-javascript-api-whats-new/