Learn about the API for HTML5 and "she" (iii)

Source: Internet
Author: User

  Web Workers (Background thread)

  JavaScript is single-threaded, and longer JavaScript operations block the UI thread.

  Web worker is a JavaScript that runs in the background and does not affect the performance of the page.

  A script executed in a Web worker cannot access the Window object Document object of the parent object.

  Web worker is typically used for tasks that consume more CPU resources.

  Detect Web Worker Support

Detects if the browser supports Web Workerif (typeof (worker)!== "undefined") {//Supports Web Workerconsole.log (' Support Web Worker ')}else{//not supported Console.log (' Web Worker not supported ')}

Create Web worker

var worker = new Worker (' demoworkers.js '); worker.onmessage = function (e) {    console.log (e.data);};/ /demoworkers.js!function () {    var i = 0;    (function () {        postMessage (++i);        SetTimeout (arguments.callee,1000);    }) ();} ();

After you create a Web worker, you typically combine the postMessage API to send and receive messages.

  Terminate Web Worker

Worker.terminate ();

Web Storage (local storage)

Previously, storing data on the client was done by a cookie. However, cookies are not suitable for storing large amounts of data because they are passed by each request to the server, which makes the cookie slow and inefficient.

The Web Storage API provides two ways to store data on the client

Localstorage-There is no time limit for stored data. After the next day, the second week, or the next year, data is still available sessionstorage-data storage for a session. When the user closes the browser window, the data is deleted.

Detect if browsing supports Web Storage

Localstorageif (window.localstorage) {//support}else{//not supported}//sessionstorageif (window.sessionstorage) {//Support}else{// Not supported}

set up and get data

Set a value of Localstorage.setitem (' OneKey ', ' onevalue '); localstorage.onekey = ' Onevalue '; Sessionstorage.setitem (' OneKey ', ' onevalue '); sessionstorage.onekey = ' onevalue ';//Get a value localstorage.getitem (' OneKey '); localstorage.onekey; Sessionstorage.getitem (' OneKey '); sessionstorage.onekey;

  Storage Properties and methods

The number of key/value pairs stored in the length//storage Object key (Index)//Gets the object at the specified position GetItem (key)//Gets the value of the specified key if no then returns Nullsetitem (Key,value)// Sets a value to the specified key position, if it already exists replace RemoveItem (key)//delete the value of the specified key clear ()//Clears all data stored in the storage object

In some complex situations, multiple pages, tabs, or workers require access to the stored data, and the application triggers a sequence of actions after the stored data has been modified. In this case, the Web Storage API provides a set of event notification mechanisms.

Window.addeventlistener (' storage ', displaystorageevent,true); function Displaystorageevent (e) {//e.key update or DELETE key// E.newvalue the value corresponding to the previous key of the update//e.oldvalue the value corresponding to the updated key//e.url storage event occurs when the source//e.storagearea points to the localstorage (or sessionstorage) that the value has changed A reference to}

application cache (app cache)

The application cache means that the Web app is cacheable and can be accessed without an Internet connection.

application caching brings three benefits to your app :

Offline Browsing-users can use their speed when the app is offline-cached resources load faster to reduce server load-The browser will download only updated or changed resources from the server.

detecting whether the browser supports app caching

if (Window.applicationcache) {//Support}else{//not supported}

enable application caching to include the manifest attribute in the of the document

The manifest file needs to be configured with the correct mime-type, or "text/cache-manifest", which must be configured on the Web server .

If you are using an Apache server, you need to modify the. htaccess file by appending one line of code: AddType text/cache-manifest. Manifest this ensures that the. manifest file has the correct MIME type, which is the text /cache-manifest. You can also include the following code in the. htaccess file: <files offline.manifest>expiresactive onexpiresdefault "Access" </Files> Add these lines of code to prevent the browser from caching the cached files. Because Offline.manifest is a static file, the browser caches the Offline.manifest file by default. So the above lines of code is to let the server tell the browser not to cache.

Manifest file

The manifest file is a simple text file that tells the browser what to cache (as well as what is not cached).

The manifest file can be divided into three parts:

Cache MANIFEST-Files listed under this heading will be cached after the first download-Files listed under this heading require a connection to the server and are not cached fallback-Files listed under this heading provide a fallback page when the page is inaccessible (for example, 404 Page

A reference example :

CACHE manifest# 2013-08-01 v1.0.0cache:/main.css/main.js/logo.gifnetwork:*fallback://offline.html/*-------------- ----------------*/manifest file must start with the cache manifest the second line is a comment stating the date and version number of the manifest file. The cache section lists all the files needed for offline use. The paths to these files are relative offline.manifest, and it is possible to use an absolute path. The network section lists all files that do not need to be cached and are not available offline. You can use the asterisk to indicate that all other resources/files require an Internet connection fallback section uses/characters define a URL template. It does this by accessing each page and asking, "Is there a page in the cache?" If there is a cache page is displayed, the cache that displays the specified offline.html file app is updated when its manifest file changes. If you update a picture, or a JavaScript function, these changes are not re-cached. Updating the date and version number in the comment line is a way to enable the browser to re-cache the file. /* ------------------------------ */

offline Behavior

if (navigator.online) {//online console.log}else{//offline console.log (' offline ')}window.addeventlistener (' online '), Function (e) {console.log (' online ');},true); Window.addeventlistener (' Offline ', function (e) {console.log (' offline ');},true);

Learn about the API for HTML5 and "she" (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.