Onclick javascript to make browser go back to previous page?

581,081

Solution 1

Add this in your input element

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

Solution 2

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"/>

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

Simple. One line.

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

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

Share:
581,081

Related videos on Youtube

Doc Holiday
Author by

Doc Holiday

I work as a Java Developer who often has to do SQL development, HQL development, and front-end styling using various JS frameworks.

Updated on August 25, 2021

Comments

  • Doc Holiday
    Doc Holiday over 2 years

    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"/>
    
    • John Strickler
      John Strickler over 12 years
      window.history.back()
  • Vadim
    Vadim over 12 years
    Put this to the button onClick handle
  • ctrlplusb
    ctrlplusb almost 10 years
    This doesn't work in all browsers for me, I had to do the following <input action="action" type="button" value="Back" onclick="window.history.go(-1); return false;" /> This answer is quite old, so it could have been an issue introduced into more modern versions of browsers. :)
  • Jeromy French
    Jeromy French about 8 years
    @JeromeJ it is a link that does nothing if JavaScript isn't enabled/working (for whatever reason) on the page. If JavaScript is working, it takes the browser back one page.
  • Adrien
    Adrien about 8 years
    Does this show the previous page from cache or reloads the previous page from the server?
  • WowThatsLoud
    WowThatsLoud almost 8 years
    You could be more clear like giving the answer in the code
  • Costa Michailidis
    Costa Michailidis about 7 years
    What's the browser support on that?
  • ban-geoengineering
    ban-geoengineering about 7 years
    What's the action="action" part for? And is it valid html??
  • ToolmakerSteve
    ToolmakerSteve about 5 years
    @Adrien - its javascript, so it is running in the browser. The browser should reuse cached page (assuming browser settings and page http header settings permit doing so).
  • Marek Bernád
    Marek Bernád over 4 years
    Works fine, but oftentimes if user decides to change language on specific page with the button like this, the back button to previous page will turn the language back, instead of page...
  • CheddarLizzard
    CheddarLizzard about 4 years
    Note that inline JS should not be used in production because: 1. JS code should be in dedicated and minified .js files 2. inline JS should be disabled through a Content Security Policy to mitigate XSS injections
  • Halfist
    Halfist almost 4 years
    Any idea why this not always working from the first time? It doesn't seem that this question was answered so far.
  • Diego Fortes
    Diego Fortes almost 3 years
    @Costa pretty good, unless you're developing to Opera Mini.
  • Josh McGee
    Josh McGee over 2 years
    some of the others didn't take me to the previous page if I had reloaded, but this one does