In this article I will explain with an example, how to redirect to Previous Page using JavaScript.
The HTML Anchor Link will be assigned an OnClick event handler and when clicked, it will be redirected to the Previous Page using history.back function in JavaScript.
 
 
HTML Markup
Page 1
The HTML Markup consists of an HTML Anchor Link whose HREF attribute is set to Page 2.
<h2>Page 1</h2>
<hr />
<a href="Page2.htm">Redirect to Page 2</a>
 
 
Page 2
The HTML Markup consists of an HTML Anchor Link.
<h2>Page 2</h2>
<hr />
<a href="javascript:;">Redirect back to Page 1</a>
 
 
Redirecting to Previous Page using JavaScript
The HTML Anchor Link in Page 2 is assigned an OnClick event handler and when clicked, it is redirected to the Previous Page using history.back function in JavaScript.
The history.back function
The history.back is an in-built JavaScript function which belongs to the JavaScript window object.
It redirects the Current Page to the Previous Page in the Browser History.
Note: If the Current Page is the First Page in History then there will be no redirection.
 
<h2>Page 2</h2>
<hr />
<a href="javascript:;" onclick = "history.back()">Redirect back to Page 1</a>
 
 
Screenshot
Redirect to Previous Page using JavaScript
 
 
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Downloads