This article recommends five new html5 API functions, including full screen API (FullscreenAPI), page visibility API (PageVisibilityAPI), getUserMediaAPI, BatteryAPI, and LinkPrefetching. I hope you can understand it for your convenience. HTML5 provides some very powerful JavaScript and HTML APIs to help developers build wonderful desktop and mobile applications. This article will introduce five new types of APIS, hoping to help your development work.
1. Full Screen API)
This API allows developers to program Web applications to run in full screen, making Web applications more like local applications.
The Code is as follows:
// Find a full screen method suitable for the browser
Function launchFullScreen (element ){
If (element. requestFullScreen ){
Element. requestFullScreen ();
} Else if (element. inclurequestfullscreen ){
Element. Required requestfullscreen ();
} Else if (element. webkitRequestFullScreen ){
Element. webkitRequestFullScreen ();
}
}
// Enable full screen mode
LaunchFullScreen(document.doc umentElement); // the whole page
LaunchFullScreen (document. getElementById ("videoElement"); // any inpidual element
2. Page Visibility API (Page Visibility API)
This API can be used to detect the visibility of a page to a user, that is, to return the status changes of the page or tag currently browsed by the user.
The Code is as follows:
// Set the name of the hidden property and visible change event. The property must contain a browser prefix.
// Since some browsers only offer vendor-prefixed support
Var hidden, state, visibilityChange;
If (typeof document. hidden! = "Undefined "){
Hidden = "hidden ";
VisibilityChange = "visibilitychange ";
State = "visibilityState ";
} Else if (typeof document. mozHidden! = "Undefined "){
Hidden = "mozHidden ";
VisibilityChange = "incluvisibilitychange ";
State = "required visibilitystate ";
} Else if (typeof document. msHidden! = "Undefined "){
Hidden = "msHidden ";
VisibilityChange = "msvisibilitychange ";
State = "msVisibilityState ";
} Else if (typeof document. webkitHidden! = "Undefined "){
Hidden = "webkitHidden ";
VisibilityChange = "webkitvisibilitychange ";
State = "webkitVisibilityState ";
}
// Add a listener with changed title
Document. addEventListener (visibilityChange, function (e ){
// Start or stop processing
}, False );
3. getUserMedia API
This API allows Web applications to access cameras and microphones without using plug-ins.
The Code is as follows:
// Set the event listener
Window. addEventListener ("DOMContentLoaded", function (){
// Obtain the element
Var canvas = document. getElementById ("canvas "),
Context = canvas. getContext ("2d "),
Video = document. getElementById ("video "),
VideoObj = {"video": true },
ErrBack = function (error ){
Console. log ("Video capture error:", error. code );
};
// Set the video listener
If (navigator. getUserMedia) {// Standard
Navigator. getUserMedia (videoObj, function (stream ){
Video. src = stream;
Video. play ();
}, ErrBack );
} Else if (navigator. webkitGetUserMedia) {// WebKit-prefixed
Navigator. webkitGetUserMedia (videoObj, function (stream ){
Video. src = window. webkitURL. createObjectURL (stream );
Video. play ();
}, ErrBack );
}
}, False );
4. Battery API)
This is an API for mobile device applications, mainly used to detect device battery information.
The Code is as follows:
Var battery = navigator. battery | navigator. webkitBattery | navigator. mozBattery;
// Battery Properties
Console. warn ("Battery charging:", battery. charging); // true
Console. warn ("Battery level:", battery. level); // 0.58
Console. warn ("Battery discharging time:", battery. dischargingTime );
// Add an event listener
Battery. addEventListener ("chargingchange", function (e ){
Console. warn ("Battery charge change:", battery. charging );
}, False );
5. Link Prefetching
Pre-load webpage content to provide a smooth browsing experience for viewers.
The Code is as follows:
The above five new types of APIs are available. Do you understand them? If you have any questions, please leave a message. Let's discuss them and make common progress together.