Coder Perfect

How can I use onclick javascript to force the browser to return to the previous page?

Problem

Is there a function I can attach to a button’s click event to force the browser to return to the previous page?

<input name="action" type="submit" value="Cancel"/>

Asked by Doc Holiday

Solution #1

This should be added to your input element.

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

Answered by rogerlsmith

Solution #2

history.back()

or

history.go(-1)

Put this in the onclick handle of the button. This is how it should look:

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

Answered by Vadim

Solution #3

Returning to the 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> 

Increase if we wish to take more than one step back.

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.......

Answered by Rizwan Gill

Solution #4

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

Answered by hspain

Solution #5

Simple. One line.

<button onclick="javascript:window.history.back();">Go Back</button>

Like Wim and Malik’s responses, but in a single sentence.

Answered by Andre Mesquita

Post is based on https://stackoverflow.com/questions/8067510/onclick-javascript-to-make-browser-go-back-to-previous-page