Onclick javascript to make browser go back to previous page?

Onclick javascript to make browser go back to previous page?

Is there a function I can attach as a click event of a button to make the browser go back to previous page?

Solutions/Answers:

Solution 1:

Add this in your input element:

<input action="action" onclick="window.history.go(-1); return false;" type="button" value="Back" />

Solution 2:

history.back()

or

history.go(-1)

Solution 3:

For Going to previous page

First Method

<a href="javascript: history.go(-1)">Go Back</a>

Second Method

<a href="##" onClick="history.go(-1); return false;">Go back</a> 

if we want to more than one step back then increase

For going 2 steps back history.go(-2)
For going 3 steps back history.go(-3)
For going 4 steps back history.go(-4)
and so on.......

Solution 4:

<input name="action" type="submit" value="Cancel" onclick="window.history.back();"/> 

Solution 5:

You just need to call the following:

history.go(-1);

Solution 6:

Shortest Yet!

<button onclick="history.go(-1);">Go back</button>

http://jsfiddle.net/qXrbx/

I prefer the .go(-number) method as then, for 1 or many ‘backs’ there’s only 1 method to use/remember/update/search for, etc.

Also, using a tag for a back button seems more appropriate than tags with names and types…

Related:  How to obtain Domain name via JavaScript? [duplicate]