5 Useful Web APIs
1. LocalStorage API - It lets you store data against the website address in the form of key/value pairs. Local storage is limited to 5 MB.
// save data in local storage
localStorage.setItem('key', ");
// get data from local storage
localStorage.getItem('key');
// remove data from local storage
localStorage.removeItem('key');
2. Geolocation API - This API provides the user's geographical latitudes and longitudes and the accuracy to which it is correct.
const getCurrentLocation = {} => {
navigator.geolocation.getCurrentPosition(
position => {
const coord = position.coords;
console.loecoord.accuracy);
console.log(coord.latitude);
console.log(coord.longitude);
},
err => {
console.log('Error: ${error.messager}');
},
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
}
) ;
};
3. Clipboard API - This API provides access to the operating system's clipboard. You can use it to build a copy button to copy/paste content.
const writeToClipboard = async () => {
try {
await navigator.clipboard.writeText(userText);
console.log('Text copied to the clipboard');
} catch (error) {
console.log(error);
}
};
const readFromClipboard = async 0 => {
try {
roost clipboardText = await navigator.clipboard.readText();
console.log(clipboardText);
} catch (error) {
console.log(error);
}
} ;
4. Fetch API - It is a browser API that lets you call REST APIs or GraphQL APIs.
async function callAPI() {
try {
coast res = await fetchCAPI', {
method: '',
headers: {},
body: {},
});
coast data = await res.json();
return data;
} catch (err) {
console.log(err);
}
}
5. History API - This API lets you go back and forth between web pages that are in your session history.
// go back in history window. history. back(); // go forward in history window. history. forward();

No comments: