Location and History API in JavaScript

Javascript 9 min min read Updated: Mar 09, 2026 Intermediate
Location and History API in JavaScript
Intermediate Topic 14 of 15

JavaScript provides powerful browser APIs that allow developers to interact with the browser's URL and navigation history. Two important APIs used for this purpose are the Location API and the History API. These APIs are part of the Browser Object Model (BOM) and help control navigation and manage browser history.

Using these APIs, developers can read the current webpage URL, redirect users to another page, or move backward and forward through the browser history.

Location API

The Location API provides information about the current URL of the webpage and allows JavaScript to redirect the browser to another page.

Key Point: The location object represents the current URL loaded in the browser.

Getting Current Page URL

JavaScript can retrieve the current webpage URL using the location.href property.

javascript console.log(location.href);
Output

The complete URL of the current webpage is displayed in the console.

Redirecting to Another Page

The location object can also be used to redirect users to another webpage.

javascript location.href = "https://example.com";
Output

The browser redirects to the specified URL.

Reloading the Current Page

The location.reload() method refreshes the current webpage.

javascript location.reload();
Output

The current webpage reloads.

Key Point: The location object can also access properties like hostname, pathname, and protocol.

History API

The History API allows JavaScript to interact with the browser's session history. It enables navigation through previously visited pages without requiring manual user interaction.

Moving Back to the Previous Page

The history.back() method moves the browser to the previous page in the history stack.

javascript history.back();
Output

The browser navigates to the previously visited page.

Moving Forward in History

The history.forward() method moves the browser forward to the next page in the history stack.

javascript history.forward();
Output

The browser moves to the next page in the history.

Moving Multiple Steps in History

The history.go() method allows navigation to a specific position in the history list.

javascript history.go(-2);
Output

The browser moves two pages back in the history.

Key Point: history.go(-1) works the same as history.back().

Difference Between Location and History API

  • Location API controls the current webpage URL.
  • History API controls navigation through the browser's history.
  • Location API is used for redirects and page information.
  • History API is used for moving backward and forward in browsing history.

Conclusion

The Location and History APIs allow JavaScript to interact with browser navigation and URL management. Developers can redirect users, reload pages, and navigate through browsing history programmatically.

These APIs are essential for building modern web applications that manage navigation dynamically.

In the next tutorial, you will learn about the Navigator Object in JavaScript, which provides information about the user's browser and device.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators