APIs you must know in Html5

APIs you must know in Html5

The following is a summary of some HTML APIs that we have learned before. There are many functions and interfaces in HTML5 that are worth understanding and learning.

Page Visibility API

Full Screen API --full Screen

Get Media API--getUserMedia

Battery API --battery

Resource preloading API--link Prefetching

Page Visibility API

This API can be used to detect the visibility of a page to the user, that is, to return the status change of the page or tab tap that the user is currently browsing. It takes effect when the browser is minimized or the tap page is switched. (If you need to switch between several webviews in the app, you can use the pageVisibility interface to monitor and process the corresponding events.)

Introduction to page visibility

【document.hidden】 This value indicates whether the page is visible. The value is a boolean value.

【document.visibilityState】 This visibilitystate can have three possible values:

[visible] Indicates that the page is the frontmost page and is not in a minimized window

[hidden] means that the page is not the front page or is in a minimized window

[prerender] Indicates that the page content is being re-rendered and the page is not visible to the user.

【isibilitychange Event】*Monitors window visibility change events

Relevant code:

  1. // Set the name of the hidden attribute and visible change event, the attribute needs to be prefixed with the browser  
  2. // since some browsers only offer vendor-prefixed support var hidden, state, visibilityChange;  
  3. if (typeof document.hidden !== "undefined" ) {
  4. hidden = "hidden" ;
  5. visibilityChange = "visibilitychange" ;
  6. state = "visibilityState" ;
  7. } else   if (typeof document.mozHidden !== "undefined" ) {
  8. hidden = "mozHidden" ;
  9. visibilityChange = "mozvisibilitychange" ;
  10. state = "mozVisibilityState" ;
  11. } else   if (typeof document.msHidden !== "undefined" ) {
  12. hidden = "msHidden" ;
  13. visibilityChange = "msvisibilitychange" ;
  14. state = "msVisibilityState" ;
  15. } else   if (typeof document.webkitHidden !== "undefined" ) {
  16. hidden = "webkitHidden" ;
  17. visibilityChange = "webkitvisibilitychange" ;
  18. state = "webkitVisibilityState" ;
  19. }
  20. // Add a title change listener  
  21. document.addEventListener(visibilityChange, function(e) {
  22. // Start or stop state processing  
  23. }, false );

The benefits of page visibility

What can we do with the changes in visibility?

We can control those pages that refresh content regularly. When the page is not visible, it will not refresh. When it is visible, it will refresh.

We can also pause and resume audio and video playback based on whether the page is visible.

We can also calculate more accurate data on the number of users on our website who stay on this page based on page visibility, rather than just opening the page but not staying on this page.

Full Screen APIFull Screen API

This API allows developers to programmatically run web applications in full screen, making web applications more like local applications. Very simple and useful API.

Introduction to Full Screen

The FullScreen API is very simple to use and has two modes:

Launching Fullscreen Mode

  1. // Find the full screen method suitable for the browser  
  2. function launchFullScreen(element) {
  3. if (element.requestFullScreen) {
  4. element.requestFullScreen();
  5. } else   if (element.mozRequestFullScreen) {
  6. element.mozRequestFullScreen();
  7. } else   if (element.webkitRequestFullScreen) {
  8. element.webkitRequestFullScreen();
  9. }
  10. }
  11. // Start full screen mode  
  12. launchFullScreen(document.documentElement); // the whole page  
  13. launchFullScreen(document.getElementById( "videoElement" )); // any individual element  

Exit FullScreen Mode Exit FullScreen Mode

  1. // Whack fullscreenfunction exitFullscreen() {  
  2. if (document.exitFullscreen) {
  3. document.exitFullscreen();
  4. } else   if (document.mozCancelFullScreen) {
  5. document.mozCancelFullScreen();
  6. } else   if (document.webkitExitFullscreen) {
  7. document.webkitExitFullscreen();
  8. }
  9. }
  10. // Cancel fullscreen for browsers that support it!  
  11. exitFullscreen();

Full Screen related properties and events

Currently, there are still compatibility issues with fullscreen, and many browsers that can use it still need to add relevant prefixes to their corresponding properties and events.

【document.fullScreenElement】 This property indicates the element that starts the full screen (such as video)

[document.fullScreenEnabled] This property indicates whether the current screen is full screen

【fullscreenchange event】 Listen for fullscreen status change events

Full Scrren related

There are some CSS properties about fullscreen in CSS

  1. -webkit-full-screen,
  2. :-moz-full-screen,
  3. :-ms-fullscreen,
  4. :full-screen {
  5. /*pre-spec */  
  6. /* properties */  
  7. }
  8. :fullscreen {
  9. /* spec */  
  10. /* properties */  
  11. }
  12. /* deeper elements */ :-webkit-full-screen video {
  13. width: 100 %;
  14. height: 100 %;
  15. }
  16. /* styling the backdrop*/ ::backdrop {
  17. /* properties */  
  18. }
  19. ::-ms-backdrop {
  20. /* properties */  
  21. }

Summary of FullScreen

The first time I saw this API was when I was looking at some mobile novels and mobile comics websites, and found that it had a full-screen viewing function. The full-screen API may have compatibility issues at present, but I believe it will definitely be a highly used API in the near future.

getUserMedia API

The API allows web applications to access the camera and microphone without the use of plug-ins. The API is fully supported on the client side, but is still not available on the PC side.

Introduction to getUserMedia API

First read the following html

  1. Snap Photo

Related JS code

  1. // Set up event listeners  
  2. window.addEventListener( "DOMContentLoaded" , function() {
  3. // Get the element  
  4. var canvas = document.getElementById( "canvas" ),
  5. context = canvas.getContext( "2d" ),
  6. video = document.getElementById( "video" ),
  7. videoObj = { "video" : true },
  8. errBack = function(error) {
  9. console.log( "Video capture error: " , error.code);
  10. };
  11. // Set up the video listener  
  12. if (navigator.getUserMedia) { // Standard  
  13. navigator.getUserMedia(videoObj, function(stream) {
  14. video.src = stream;
  15. video.play();
  16. }, errBack);
  17. } else   if (navigator.webkitGetUserMedia) { // WebKit-prefixed  
  18. navigator.webkitGetUserMedia(videoObj, function(stream){
  19. video.src = window.webkitURL.createObjectURL(stream);
  20. video.play();
  21. }, errBack);
  22. }
  23. }, false );

Once we have confirmed that the current browser supports getUserMedia, we can use a simple method to assign the src video address of our current video element to the local video of the user's mobile phone, and then use the video's play method to start and connect the local video. In this way, we can use the local player to play it.

Battery API

This is an API for mobile device applications, mainly used to detect device battery information.

Introduction to Battery API

  1. var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery;
  2. //Battery properties  
  3. console.warn( "Battery charging: " , battery.charging); // Is the battery currently charging true  
  4. console.warn( "Battery level: " , battery.level); // 0.58  
  5. console.warn( "Battery discharging time: " , battery.dischargingTime);
  6. // Add event listener  
  7. battery.addEventListener( "chargingchange" , function(e) {
  8. console.warn( "Battery charge change: " , battery.charging);
  9. }, false );

Why get battery information API

Why do we need to use the battery API? Nowadays, many mobile apps are embedded with web browsers (no longer completely native applications). So we need a way to get system information. Some processes in the app are very power-consuming, so we need to give users some warning messages when they start the app to tell them that the current battery level of the device is low. This is a very important and simple API. It will play its due role in the near future.

Link Prefetching

Preload web page content to provide a smooth browsing experience for viewers. We occasionally use this API in our business.

What is link preloading?

Link prefetching is a browser mechanism that uses the browser's latest time to download or preload documents that the user may browse in the near future.

  1. !-- full page --]
  2. [link rel= "prefetch" href= "http://davidwalsh.name/css-enhancements-user-experience" /]
  3. [!-- just an image --]
  4. [link rel= "prefetch" href= "http://davidwalsh.name/wp-content/themes/walshbook3/images/sprite.png" /]

When to use link preloading Whether to use preloading on your own website, you can refer to the following points:

When you are making a slideshow-like web page, you need to load nearly 1-3 pages in advance (assuming these pages are not large)

Preload images that are used by many pages on your site

Preload the results page for site searches

<<:  Why don’t startups choose to develop Android apps first?

>>:  Ctrip and Qunar’s marriage: Can the promised independent development be achieved?

Recommend

The Bronze, Silver and Golden Ages of Chinese Makers

The word "maker" comes from the transla...

Douyin DTV advertising marketing value white paper!

Advertisers' marketing demands are constantly...

New media operation: graphic design, in-depth IP creation

For operational promotion, the most important thi...

Across 4,600 kilometers! Space-ground integrated quantum communication

The space-ground integrated quantum communication...

How to build a user reach system?

The connotation of user growth lies in "user...

Take a photo! Tonight, Mars and the Moon will be in the same frame!

Recently, the brightness of Mars has been increas...

8P Theory Essential for Event Planning

We all know that planning is divided into many ty...

2023, the hottest year on record?

2023 could be the hottest year on record Recently...