APIs Available in HTML5IntroductionThe full form of API is the Application Programming interface. An API is a collection of pre-built programs that can be used with the help of JavaScript. With the help of API, we can implement already-written code to fulfill the needs of the project you are working on. Now, we are going to discuss some useful and popular APIs provided by HTML5. Let's see a javascript code: HTML Geolocation APIWith the help of geolocation API, we can get the current location of the user or the page visitor. Also, with the help of this API, we can see the user's location only if the user allows it to do so, as it compromises the security and privacy of the user. Syntax:Methods available in Geolocation API: There are so many methods available in the geolocation API. These are as follows. 1. getCurrentPosition() Method: This method helps us obtain the object with properties such as latitude, longitude, accuracy, and altitude. 2. watchPosition() Method: With the help of this method, we can get the current position of the user as well as the updated location when the position of the user changes or the user travels from one location to another location. 3. clearWatch() Method: with the help of this method, we can stop the watchPosition() method from tracing the user anymore. Example 1:Output: When the user clicks on the get location button, then we get the below output. Explanation: In the above code, we have implemented the logic so that we get the current latitude and longitude of the user. HTML Drag and Drop APIThis is the most common feature nowadays. With the help of this feature, we can drag an item from one place and drop it in another. Syntax:With the help of the below syntax, we can make the element draggable as shown below: Let's see an example and understand this properly. HTML code:Output: After dragging the image, we can get the below output: HTML Web Storage APIWith the help of HTML Web Storage API, we can store the data on the web browser. In earlier days, we can store the data in the form of cookies that can store a small amount of data and cannot transfer it further to the server. But when HTML 5 comes into the picture, with the help of the HTML web storage API, we can store large amounts of data compared to cookies and transfer them to the server. Using this API for storing data is more secure than using cookies. There are two objects available in the HTML web storage API. These are as follows: 1. sessionStorage: With the help of this object, we can temporarily store the data on the web browser such that if the browser is refreshed or closed, the data stored will be lost. 2. localStorage: With the help of this object, we can store the data permanently on the browser with no expiration so that it will not be lost even if the browser is refreshed. Let us understand this by taking an example. HTML code:Output: When we click on the get stored data, then the message will be displayed as per the below output. When we click on Remove Stored Data, then the message will be removed as per the output below. HTML Web Worker APIWhen we try to upload a page, the page gets stuck until uploading is finished. But with the help of Web Worker API, we can upload the JavaScript without affecting the performance of the page. It helps to run JavaScript in the background independent of other scripts. Let us understand this by taking an example. HTML code:Output: Basic APIs in HTML5Let's see some more basic APIs available in HTML5. 1. Ambient Light API: This API allows us to get information about the ambient light levels detected by a device's light sensor. 2. Battery Status API: With the help of this API, we can get information about the battery status of the device. 3. Canvas 2D Context: With the help of this API, we can draw and manipulate the graphics in a browser. 4. Clipboard API: This API allows us to access the operating system's copy, cut, and paste functionality. 5. Contacts: With the help of this API, we can access the user's contacts repository in the web browser. 6. Drag and Drop: With the help of this API, we can drag and drop items within and between browser windows. 7. File API: With the help of this API, we can Provide programs with secure access to the device's file system. 8. Forms: With the help of this API, we can Give programs access to the new data types defined in HTML5. 9. Fullscreen API: This API allows us to control the use of the user's full screen for web pages without the browser user interface. 10. Gamepad API: With the help of this API, we can provide support input from USB gamepad controllers. 11. Geolocation: With the help of this API, we can Provide web applications with access to geographical location data about the user's device. 12. getUserMedia/Stream API: This API allows us to provide access to external device data (such as webcam videos). 13. History API: With the help of this API, we can Allow programs to manipulate the browser history. 14. HTML Microdata: With the help of this API, we can Provide a way to annotate content with computer-readable labels. 15. Indexed database: With the help of this API, we can Create a simple client-side database system in the web browser. 16. Internationalization API: With the help of this API, we can provide access to locale-sensitive formatting and string comparison. 17. Offline apps: With the help of this API, we can Allow programmers to make web apps available in offline mode. 18. Proximity API: With the help of this API, we can Provide information about the distance between a device and an object. 19. Screen Orientation: With the help of this API, we can read the screen orientation state (portrait or landscape) and give programmers the ability to know when it changes and lock it in place. 20. Selection: With the help of this API, we can Support selecting elements in JavaScript using CSS-style selectors. 21. Server-sent events: With the help of this API, we can Allow the server to push data to the browser without the browser needing to request it. 22. User Timing API: With the help of this API, we can Give programmers access to high-precision timestamps to measure the performance of applications. 23. Vibration API: With the help of this API, we can Allow access to the vibration functionality of the device. 24. Web Audio API: With the help of this API, we can use API for processing and synthesizing audio. 25. Web messaging: With the help of this API, we can Allow browser windows to communicate with each other across different origins. 26. Web Speech API: With the help of this API, we can Provide speech input and text-to-speech output features. 27. Web storage: With the help of this API, we can Allow the storage of key-value pairs in the browser. 28. Web socket: With the help of this API, we can Open an interactive communication session between the browser and server. 29. Web Workers: With the help of this API, we can Allow JavaScript to execute scripts in the background. 30. XMLHttpRequest2: With the help of this API, we can improve XMLHttpRequest to eliminate the need to work around the same-origin policy errors and to make XMLHttpRequest work with the new features of HTML5. Next TopicBrackets around HTML Tags |