JavaScript Web APIS
JavaScript Web APIS
fetch(`https://api.openweathermap.org/data/2.5/weather?q
=${city}&appid=${apiKey}&units=metric`)
.then(response => response.json())
.then(data => {
console.log(`Current temperature in ${city}:
${data.main.temp}°C`);
console.log(`Weather: ${data.weather[0].description}`);
})
.catch(error => console.error('Error fetching weather:',
error));
JavaScript Web API List
• Here, we have listed the most common web APIs.
Web History API
• In JavaScript, the history API allows us to access the browsers history. It can be
used to navigate through the history.
• JavaScript History API provides us with methods to manipulate window history
object. History object is a property of JavaScript window object. The window
history object contains the array of visited URLs in the current session
• The history API is very powerful tool to create may useful effects. For
example, we can use it to implement history based undo redo system.
How to use JavaScript History API?
The History API is a very simple API to use. There are just a few methods and a
property that you need to know about:
Syntax
• Followings are the syntaxes to use the different methods
and property of the history object −
Loading Previous Page in History List
• The JavaScript history back() method of the history object loads the previous URL in
the history list. We can also use the history go() method to load the previous page.
The difference between these two methods is that back() method only load the
immediate previous URL in history list but we can use the go() method to load any
previous URL in the history list.
Example
Example
In the code in right hand side, we fetch the data
from the given URL using the fetch() API. It
returns the promise we handle using the 'then'
block.
First, we convert the data into the JSON format.
After that, we convert the data into the string and
print it on the web page.
Fetch with Async/Await (Modern Approach)
Example
In the code right hand side, we have defined the
getData()asynchronous function to fetch the to-do
list data from the given URL using the fetch() API.
After that, we convert the data into JSON and print
the output on the web page.
Options of Fetch API
Example: Making a GET Request
• In the below code, we have passed the "GET" method as an option to the fetch() API.
• Fetch () API fetches the data from the given API endpoint.
Advantages of Using the Fetch() API
Geolocation API
• The Geolocation API is a web API that provides a JavaScript interface to
access the user's geographical location data. A Geolocation API
contains the various methods and properties that you can use to
access the user's location on your website.
• It detects the location of the user's using the device's GPS. The
accuracy of the location depends on the accuracy of the GPS device.
• As location compromises the users' privacy, it asks for permission
before accessing the location. If users grant permission, websites can
access the latitude, longitude, etc.
• Sometimes, developers need to get the user's location on the website.
For example, if you are creating an Uber,Kakaotaxi etc. type
applications, you need to know the user's location to pick them up for
the ride.
• The Geolocation API allows users to share the location with a particular
website.
Real-time use cases of the Geolocation API
• Here are the real-time use cases of the Geolocation
API.
Using the Geolocation API
• To use the Geolocation API, you can use the 'navigator' property of the window object. The Navigator object
contains the 'geolocation' property, containing the various properties and methods to manipulate the user's
location.
Example
The code in the right hand side finds the most accurate
location. Also, we have set milliseconds to the maximum
Age, and timeout properties of the options object.
First, we convert the data into the JSON format. After that,
we convert the data into the string and print it on the web
page.
Watching the Current Location of the User
Example
• In the code right hand side, we used the
geolocation object's watchPosition ()
method to get the user's continuous
position.
• We used the getCords() function as a
callback of the watchPosition() method,
where we print the latitude and longitude
of the user's position.
• In the findLocation() method, we used the
setTimeOut() method to stop tracking after
30 seconds.
• In the output, you can observe that the
code prints the user's position multiple
times.
• First, we convert the data into the JSON
format. After that, we convert the data into
the string and print it on the web page.
Exercise