Doc Holiday
Doc Holiday

Reputation: 10254

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?

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

Upvotes: 335

Views: 714845

Answers (14)

Taib Islam Dipu
Taib Islam Dipu

Reputation: 481

You could use one of the following :

history.back() or window.history.back()

You also go back to N number of pages with this one
history.go(-1)

Upvotes: 1

Gaurav Patil
Gaurav Patil

Reputation: 1382

Access window.history and then call back()

window.history.back()

Upvotes: 8

DevTheJo
DevTheJo

Reputation: 2477

the only one that worked for me:

function goBackAndRefresh() {
  window.history.go(-1);
  setTimeout(() => {
    location.reload();
  }, 0);
}

Upvotes: 1

Fezal halai
Fezal halai

Reputation: 784

100% work

<a onclick="window.history.go(-1); return false;" style="cursor: pointer;">Go Back</a>

Upvotes: -1

Mannu saraswat
Mannu saraswat

Reputation: 1081

Works for me everytime

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

Upvotes: 17

rogerlsmith
rogerlsmith

Reputation: 6786

Add this in your input element

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

Upvotes: 552

Vadym
Vadym

Reputation: 5284

history.back()

or

history.go(-1)

Put this to the button onclick handle. It should look like this:

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

Upvotes: 163

Andre Mesquita
Andre Mesquita

Reputation: 918

Simple. One line.

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

Like Wim's and Malik's answer, but just in one line.

Upvotes: 17

Wim Mostrey
Wim Mostrey

Reputation: 277

This is the only thing that works on all current browsers:

<script>
function goBack() {
    history.go(-1);
}
</script>
<button onclick="goBack()">Go Back</button>

Upvotes: 6

Malik Khalil Ahmad
Malik Khalil Ahmad

Reputation: 6915

window.history.back();

<button onclick="goBack()">Go Back</button>

<script>
function goBack() {
    window.history.back();
}
</script>

Upvotes: 16

Rizwan Gill
Rizwan Gill

Reputation: 2253

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

Upvotes: 117

Michael Durrant
Michael Durrant

Reputation: 96454

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

Upvotes: 14

Rich O&#39;Kelly
Rich O&#39;Kelly

Reputation: 41757

You just need to call the following:

history.go(-1);

Upvotes: 12

hspain
hspain

Reputation: 17568

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

Upvotes: 16

Related Questions