Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Brtest Tutorial


JavaScript Browser API

The Browser API (Application Programming Interface) lets JavaScript interact with the web browser and the user’s environment. You can store data, get user location, open new windows, and more.


What Is the Browser API?

APIs (Application Programming Interfaces) are built into browsers to let JavaScript perform advanced tasks beyond the web page itself. Common browser APIs include:

  • Web Storage API - Save and retrieve data in the browser
  • Geolocation API - Get the user's location
  • History API - Navigate browser history
  • Window API - Work with the browser window
  • Fetch API - Request data from web servers

Web Storage API

The Web Storage API provides two storage objects:

  • localStorage — stores data with no expiration date
  • sessionStorage — stores data until the browser is closed

Example

Save and retrieve data with localStorage:

<button onclick="saveName()">Save Name</button>
<button onclick="showName()">Show Name</button>
<p id='output'></p>
<script>
function saveName() {
  localStorage.setItem('username', 'Bo');
}
function showName() {
  const name = localStorage.getItem('username');
  document.getElementById('output').textContent = name ? 'Hello, ' + name : 'No name saved';
}
</script>
Try it Yourself »

Tip: Use localStorage.removeItem('key') or localStorage.clear() to delete stored data.


Session Storage

sessionStorage works the same way as localStorage, but the data disappears when you close the tab.

Example

Using sessionStorage:

<button onclick="countVisit()">Count Visit</button>
<p id='count'></p>
<script>
function countVisit() {
  let count = sessionStorage.getItem('visits') || 0;
  count++;
  sessionStorage.setItem('visits', count);
  document.getElementById('count').textContent = 'Visits this session: ' + count;
}
</script>
Try it Yourself »

Geolocation API

The Geolocation API lets you get the user's location (with their permission). This can be useful for maps, weather updates, or location-based content.

Example

Get your coordinates:

<button onclick="getLocation()">Get My Location</button>
<p id='loc'></p>
<script>
function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition, showError);
  } else {
    document.getElementById('loc').textContent = 'Geolocation not supported';
  }
}

function showPosition(pos) {
  document.getElementById('loc').textContent = 'Latitude: ' + pos.coords.latitude + ', Longitude: ' + pos.coords.longitude;
}

function showError() {
  document.getElementById('loc').textContent = 'Unable to retrieve location.';
}
</script>
Try it Yourself »

Note: The Geolocation API requires a secure (HTTPS) connection to work.


History API

The History API lets you navigate the browser’s session history and change URLs dynamically.

Example

Move back and forward:

<button onclick="goBack()">Go Back</button>
<button onclick="goForward()">Go Forward</button>
<script>
function goBack() { window.history.back(); }
function goForward() { window.history.forward(); }
</script>
Try it Yourself »

Window API

The window object represents the browser window and allows you to open, close, and resize windows or interact with alerts and prompts.

Example

Working with window methods:

alert('Hello from JavaScript!');
const name = prompt('What is your name?');
if (name) alert('Welcome, ' + name + '!');

// Open a new window
// const newWin = window.open('https://www.w3schools.com', '_blank');
Try it Yourself »

Fetch API

The Fetch API is used to make HTTP requests to servers and work with data such as JSON.

Example

Get data from an API:

fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));
Try it Yourself »

Best Practices

  • Always check if an API is available before using it (e.g., if (navigator.geolocation)).
  • Use try...catch or .catch() to handle errors safely.
  • Store only simple data (strings, numbers, JSON) in localStorage.
  • Respect user privacy when accessing location or personal data.

Summary

  • Browser APIs extend what JavaScript can do inside the browser.
  • Web Storage saves data locally or per session.
  • Geolocation gets user coordinates (with permission).
  • History and Window APIs let you control navigation and interaction.
  • Fetch API lets you get and send data to servers.

Next, you’ll use these browser features to build a Weather App Project that fetches real-time weather data using the Fetch and Geolocation APIs.

Next » Project: Weather App

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.