Home >Web Front-end >H5 Tutorial >Introduction to 5 HTML5 interfaces you don't know_html5 tutorial skills

Introduction to 5 HTML5 interfaces you don't know_html5 tutorial skills

WBOY
WBOYOriginal
2016-05-16 15:48:581961browse

Original address: 5 HTML5 APIs You Didn't Know Existed
Original date: September 27, 2010
Translation date: August 7, 2013
When people see or When the word "HTML5" is said, it is estimated that at least half of the people will think of her as a sexy and charming beauty, but also a unicorn that can make you miserable. No wonder We developers?
We noticed that the basic APIs have been stagnant for such a long time (probably 1999-2009) that even a basic additional function like "placeholder" will take us a long time to process.
Although the current mainstream browsers have implemented many new HTML5 features, many developers have simply not noticed these simpler and more useful APIs.
This series of articles introduces these interface APIs, and also hopes to encourage more developers to explore APIs that are not yet widely known.
Element.classList
This attribute has been released for several years. Through classList, we can manipulate the class attribute of the underlying css through JavaScript.
The code is as follows:

Copy code
The code is as follows:

// Use the classList attribute (Dom element, css class name)
function toggleClassList(element,cName){
// 1. classList API
// Switch the class, remove it if it exists, add it if it doesn’t
if(element.classList.toggle){
element .classList.toggle(cName);
return true;
}
// !!! In fact, if this function toggleClassList is supported,
// then the following code will not be executed. This is just for demonstration, please use it flexibly
// 2. classList API
// Whether the class attribute of element contains hide this CSS class
var hasHide = element.classList.contains(cName);
//
if(hasHide){
// 3. classList API
// Remove hide class
element.classList.remove(cName);
} else {
// 4. classList API
// Add hide class
element.classList.add(cName);
}
return true;
};

ContextMenu API
After testing, chrome28 does not work. . .
The new API, ContextMenu, is an excellent interface: This interface allows you to easily add menu items to the browser's context menu (right-click menu), instead of overriding the browser's default right-click menu.
It should be noted that you'd better use js script to dynamically create the menu contextmenu, so as to avoid redundant HTML code when the page disables JS script.
The code is as follows:

Copy the code
The code is as follows:





Click this area to view the menu













Element.dataset
Dataset (dataset) API allows developers to set (set) and get (get) attribute values ​​starting with the data- prefix on DOM elements.
The code is as follows:

Copy the code
The code is as follows:




Copy code
The code is as follows:
function testDataset(){
//
var intro = document.getElementById("intro");
// Note that this is not the id attribute, but the value of data-id
var id = intro.dataset.id;
// data-website
var website = intro.dataset.website;
// data-blog-url, camel case..
var blogUrl = intro.dataset.blogUrl;
// data-my-name
var myName = intro.dataset.myName;
//
var msg = "qq:" id
",website:" website
",blogUrl:" blogUrl
",myName:" myName
;
//
warn(msg);
};

Nothing to say, just like classList, simple but practical. (Think about it, has it changed some interactions and decoupling of background and front-end JS?)

window.postMessage API
IE8 has supported postMessage API for several years, this API allows window and iframe elements to communicate with each other.
Cross-domain support. The code is as follows:


Copy code The code is as follows:
// From window or frame on domain 1, send a message to the iframe which hosts another domain
var iframeWindow = document.getElementById("iframe").contentWindow;
iframeWindow.postMessage("Hello from the first window!");
// From inside the iframe on different host, receive message
window.addEventListener("message", function(event) {
// Make sure we trust the sending domain
if(event.origin = = "http://davidwalsh.name") {
// Log out the message
console.log(event.data);
// Send a message back
event.source.postMessage ("Hello back!");
}
]);
// message only allows string type data, but you can use JSON.stringify and JSON.parse to pass more meaningful messages.


autofocus Attribute The autofocus attribute ensures that a given BUTTON, INPUT or TEXTAREA element automatically gets focus when the page loads.


Copy codeThe code is as follows:




autofocus Attributes are mainly used in simple input pages. For details, please refer to: autofocus attribute
Each browser manufacturer has different support for these APIs, so it is best to check the compatibility before using it and spend some time reading the above. Listed APIs, you will learn more about them and master them. The test code for the
part is as follows:


Copy the codeThe code is as follows:




5 HTML5 API interface demonstrations you don’t know




<script> <br>// Display warning message<br>function warn(msg){ <br>warn = warn || "An unknown warning!" ; <br>if(window.console){ <br>console.warn(msg); <br>} else { <br>alert(msg); <br>} <br>}; <br>// Use classList attribute (Dom element, css class name) <br>function toggleClassList(element, cName){ <br>// 1. classList API <br>// Switch classes, remove them if they exist, add them if they don’t exist <br>if (element.classList.toggle){ <br>element.classList.toggle(cName); <br>return true; <br>} <br>// !!! In fact, this function toggleClassList if supported, <br> // Then the following code will not be executed. It is only for demonstration. Please apply it flexibly <br>// 2. classList API <br>// Whether the class attribute of element contains hide this CSS class <br>var hasHide = element.classList.contains(cName); <br>// <br>if(hasHide){ <br>// 3. classList API <br>// Remove hide class <br>element.classList.remove( cName); <br>} else { <br>// 4. classList API <br>// Add hide class <br>element.classList.add(cName); <br>} <br>return true; <br>}; <br>// Use className attribute (Dom element, css class name) <br>function toggleClassName(element, cName){ <br>var className = element.className || ""; <br>// Remove Blank spaces at the beginning and end <br>cName = cName.replace(/^s*|s*$/g,""); <br>// If there are blank characters in the middle of cName, it will fail. If you want to handle it well, you can split it For arrays, process them individually <br>var blankReg = /s /; <br>if(blankReg.test(cName)){ <br>warn("'" cName "'contains blank characters in the middle"); <br>return false; <br>} <br>// Regularly, b represents the boundary of visible continuous characters, which can be understood like this: <br>// "hide2 hide hide myname" Then, <br>// There is a virtual one before and after hide2 There is also b before and after hide, <br>// but not between hi and de. <br>// g represents a single-line global <br>//var rep = /bhideb/g; <br>var rep = new RegExp("\b" cName "\b", "g"); <br>if (rep.test(className)){ <br>className = className.replace(rep,""); <br>} else { <br>className = " " cName; <br>} <br>// Replace new className. <br>element.className = className; <br>return true; <br>}; <br>// Function, toggle (element id, className) <br>function toggleClass(elementId, cName){ <br>// Get a DOM element <br>var element = document.getElementById(elementId); <br>// If the element does not exist <br>if(!element){ <br>warn("The element with id " elementId " does not exist "); <br>return false; <br>} <br>if(!element.classList){ <br>warn("The element with id " elementId " does not support the classList attribute and will use other means to achieve it") ; <br>return toggleClassName(element,cName); <br>} else { <br>return toggleClassList(element,cName); <br>} <br>}; <br>function testDataset(){ <br>/ / <br>var intro = document.getElementById("intro"); <br>// Note that this is not the id attribute, but the value of data-id <br>var id = intro.dataset.id; <br>/ /data-website <br>var website = intro.dataset.website; <br>// data-blog-url, camel case.. <br>var blogUrl = intro.dataset.blogUrl; <br>// data -my-name <br>var myName = intro.dataset.myName; <br>// <br>var msg = "qq:" id <br> ",website:" website <br> ",blogUrl:" blogUrl <br> ",myName:" myName <br>; <br>// <br>warn(msg); <br>}; <br>// Execute after dom is loaded <br>window.addEventListener("DOMContentLoaded" , function() { <br>var open = document.getElementById("open"); <br>var close = document.getElementById("close"); <br>open.addEventListener("click",function(){ <br>// <br>toggleClass("diary2","hide"); <br>toggleClass("loading","hide"); <br>}); <br>close.addEventListener("click", function(){ <br>// <br>toggleClass("diary2","hide"); <br>toggleClass("loading","hide"); <br>}); <br>// <br>testDataset(); <br>}, false); <br></script>