How to navigate between web pages using javascript history API ? Last Updated : 23 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we’ll look at how to use history API to navigate between pages. We use History API to navigate programmatically between web pages. History API provides us with a way to access the browser's session history. It exposes useful methods and properties that let you navigate back and forth through the user's history, and manipulate the contents of the history stack.Methods in History API:1. window.history.back: This is the same as clicking the back button of the browser.Syntax:window.history.back()2. window.history.forward: This is the same as clicking the forward button.Syntax:window.history.forward()3. window.history.go: This can be used to jump to another page specified as a number. The current page is represented by the number 0. So, the negative number is any page before and the positive number is the page after.Syntax:window.history.go(number)Example 1: Now, let's implement the above-given methods to navigate between pages. We have two HTML files here and we are able to move between these files using back-and-forward methods.Create two files with name base.html and app.html and paste the below codesClick on the link firstClick on the buttons to navigatebase.html HTML <!DOCTYPE html> <html> <head> <title>NAVIGATION</title> </head> <body> <h1 style="color:green"> From GEEKS FOR GEEKS to HELLO WORLD </h1> <a href="file:///E:/codes/app.htm">link 1</a> <button onclick="window.history.forward()"> FORWORD </button> </body> </html> app.html HTML <!DOCTYPE html> <html> <head> <title>NAVIGATION</title> </head> <body> <h1 style="padding:10px;color:blue"> From HELLO WORLD to GEEKS FOR GEEKS </h1> <button onclick="window.history.back()"> BACK</button> </body> </html> Output: Example 2: Let's implement go() method with the same example above:Create two files with name base.html and app.html and paste the below codesClick on the link firstClick on the go button to navigate backbase.html HTML <html> <head> <title>NAVIGATION</title> </head> <body> <h1 style="color:green"> From GEEKS FOR GEEKS to HELLO WORLD </h1> <a href="file:///E:/codes/app.htm">link 1</a> </body> </html> app.html HTML <!DOCTYPE html> <html> <head> <title>NAVIGATION</title> </head> <body> <h1 style="padding:10px;color:blue"> From HELLO WORLD to GEEKS FOR GEEKS </h1> <button onclick="window.history.go(-1)">GO(-1)</button> </body> </html> Output: Comment More infoAdvertise with us Next Article How to navigate between web pages using javascript history API ? N nikhilkalburgi Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Stop Browser Back Button using JavaScript ? The browser back button allows users to go back to the previous page in their browsing history. It is an essential navigation feature, but there are times when you might want to prevent users from leaving a page. Here are several methods to prevent or control back navigation using JavaScript. 1. Red 5 min read How to Get Browser to Navigate URL in JavaScript? As a web developer, you may need to navigate to a specific URL from your JavaScript code. This can be done by accessing the browser's window object and using one of the available methods for changing the current URL.In JavaScript, there are several approaches for navigating to a URL. The most common 4 min read How to Redirect to Another Webpage using JavaScript? JavaScript can redirects the users from current page to another webpage (different URL) with and without requiring any manual action (like clicking a link). To redirect to another weboage in JavaScript we need to manipulate the window.location object and it will allow us to navigate programmatically 2 min read How to make browser to go back to previous page using JavaScript ? There are two popular methods to navigate a browser back to the previous page. These methods allow easy navigation in a web session's history, offering simple and effective ways to move backward or forward through a user's browsing history.These are the following two ways: Table of ContentUsing hist 3 min read How to navigate URL in an iframe with JavaScript ? To navigate the URL in an iframe with JavaScript, we have to set the src attribute or return the value of the src attribute in an iframe element. The src attribute defines the URL of the document that can be shown in an iframe.Syntax:document.getElementById("selector").src = "URL";URL values: Absolu 1 min read Like