JS Tutorial
JS Version
JS Objects
JS Function
JS Classes
JS Async
JS HTML DOM
JS Browser BOM
JS Web API
JS AJAX
JS JSON
JS vs JQUERY
JS Graphics
Web APIs – Intro
What is Web API?
- API full form is Application Programming Interface.
- A Web API acts as an application programming interface for the Web/
- A Browser API can extend the functionality of a web browser.
- A Server API can extend the functionality of a web server.
Browser APIs
All browsers consist of a set of built-in Web APIs to support complex operations and to help access data.
For example, the Geolocation API can return the coordinates of where the browser is located.
Example
Get the latitude and longitude of the user’s position:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Geolocation</h2>
<p>Click the button to get your coordinates.</p>
<button onclick=”getLocation()”>Try It</button>
<p id=”demo”></p>
<script>
const x = document.getElementById(“demo”);
function getLocation() {
try {
navigator.geolocation.getCurrentPosition(showPosition);
} catch {
x.innerHTML = err;
}
}
function showPosition(position) {
x.innerHTML = “Latitude: ” + position.coords.latitude +
“<br>Longitude: ” + position.coords.longitude;
}
</script>
</body>
</html>
Output
JavaScript Geolocation
Click the button to get your coordinates.
Third-Party APIs
Third-party APIs are not built into your browser.
To use these APIs, you will have to download the code from the Web.
Examples
YouTube API – enables you to display videos on a website
Twitter API – enables you to display Tweets on the website
Facebook API – This allows you to display Facebook info on a website.