Web Designing Tutorials

How to go Back Browsing History Using JavaScript

Pinterest LinkedIn Tumblr

The history property of the window object refers to the history object for the window. Using a history property you can go back to the previous browsing history using JavaScript. The history object models the browsing history of a window. A list of documents, document states, and the length property of the history object specifies the number of elements in the browsing history list.

The history object has back() and forward() methods that behave like the browser’s back and forward buttons do. Implementation of those methods makes the browser go backward or forward one step in its browsing history.

history.back();     // Go back to the browsing history
history.forward(); //Go forward to the browsing history

Also Read: How to Scroll Top or Bottom of Document Using JavaScript

Creating Buttons for History Back and Forward

You can create buttons to navigate browsing history back and forward using the following code.

<input type=button name="back" value="Go Back" onclick="history.back();">

<input type=button name="forward" 
value="Go Forward"onclick="history.forward();">

Preview:

The “Go Back” button below moves one step back to the browsing history and the “Go Forward” button moves one step forward to the browsing history.


A third method, go(), takes an integer argument and can skip any number of pages forward or backward in the history list.

history.go(-2) //go back 2, like clicking the back button twice

Also Read: How to Select Document Elements Using JavaScript?

Creating Buttons for History Back and Forward for Specific Page

You can create buttons to navigate browsing history two-step back using the following code.

<input type=button name="backward2"
value="Go Back 2" onclick="history.go(-2);">

Preview:

Clicking the button below moves two-step back from the browsing history.


Using history.go() method, if you want to go forward, you can use positive arguments. Similarly, if you want to go back, you can use a negative argument and the number used on the argument represents how many steps to go back or forward.

For example, if you want to go two steps forward to the browsing history, you can use history.go(2); and if you want to go three-step backward to the browsing history, can use history.go(-3);

If a window contains child windows such as <iframe> elements, the browsing histories of the child windows are chronologically interleaved with the history of the main window.  This means that calling history.back() method on the main window may cause one of the child windows to navigate back to a previously displayed document but leave the main window in its current state.

If a frame is contained within another frame that is contained within a top-level window. This frame can refer to the top-level window as a parent. For a parent window to navigate back to a previously displayed document you can use parent property as below.

parent.history.back();

I have described here some methods of navigating browsing history back and forward using JavaScript. If you know other more methods of navigating browsing history back and forward using JavaScript, you are welcomed to mention the comment session at the bottom of this post.

Read Next: How to Show Pop Up Window Using JavaScript

Author

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 8 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Comments are closed.