Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Onclick javascript to make browser go back to previous page?

People also ask

How do I go back to previous page in JavaScript?

The history. back() method loads the previous URL (page) in the history list. The history. back() method only works if a previous page exists.

Which button takes you to the previous page in browser?

When you hit the back button in your browser, or(alt+left) in chrome, the browser actually just loads the cached HTML file in the history. it doesn't send another GET request to the server, so when you go back in some ecommerce website and pass the password again it will throw exception to you.

Which button takes you back to the previous page?

Answer. Left side button is used by default to go back in a page.


Add this in your input element

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

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

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

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

Simple. One line.

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

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


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


window.history.back();

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

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