Back to previous page javascript

Window history.back()

Example

Create a back button on a page:


The output of the code above will be:

Click on Go Back to see how it works.
(Will only work if a previous page exists in your history list)

Try it Yourself »


Definition and Usage

The history.back() method loads the previous URL (page) in the history list.

The history.back() method only works if a previous page exists.


Syntax

Parameters

Return Value


Browser Support

history.back() is supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes

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


Back to previous page javascript

asked Nov 9, 2011 at 15:43

Doc HolidayDoc Holiday

9,59831 gold badges96 silver badges150 bronze badges

1

Add this in your input element


kartikluke

2,2971 gold badge18 silver badges32 bronze badges

answered Nov 9, 2011 at 15:45

Back to previous page javascript

6

history.back()

or

history.go(-1)

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


Back to previous page javascript

ˈvɔlə

8,1079 gold badges60 silver badges82 bronze badges

answered Nov 9, 2011 at 15:44

VadimVadim

5,0142 gold badges19 silver badges18 bronze badges

4

For Going to previous page

First Method

Go Back

Second Method

Go back 

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

answered Jun 3, 2013 at 10:52

Back to previous page javascript

Rizwan GillRizwan Gill

2,1431 gold badge16 silver badges27 bronze badges

1

 

answered Nov 9, 2011 at 15:44

Back to previous page javascript

hspainhspain

17.5k5 gold badges18 silver badges31 bronze badges

0

Simple. One line.


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

answered May 12, 2018 at 20:01

Back to previous page javascript

1

Shortest Yet!


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

answered Nov 9, 2011 at 16:14

Michael DurrantMichael Durrant

90.7k91 gold badges311 silver badges475 bronze badges

0

window.history.back();




answered Nov 29, 2016 at 10:05

Back to previous page javascript

Malik KhalilMalik Khalil

6,0052 gold badges38 silver badges32 bronze badges

Works for me everytime


    

Back to previous page javascript

CroMagnon

1,1987 gold badges20 silver badges32 bronze badges

answered Oct 14, 2019 at 13:04

Back to previous page javascript

You just need to call the following:

history.go(-1);

answered Nov 9, 2011 at 15:44

Back to previous page javascript

Rich O'KellyRich O'Kelly

40.6k9 gold badges83 silver badges113 bronze badges

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



answered Jan 26, 2017 at 13:57

Wim MostreyWim Mostrey

2675 silver badges8 bronze badges

Access window.history and then call back()

window.history.back()

answered Aug 24, 2021 at 12:31

1

the only one that worked for me:

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

answered Feb 17, 2021 at 18:15

DevTheJoDevTheJo

2,0192 gold badges20 silver badges22 bronze badges

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)

Back to previous page javascript

answered Sep 3 at 23:48

Back to previous page javascript

2

100% work

Go Back

answered Jul 30, 2020 at 2:37

Back to previous page javascript

Fezal halaiFezal halai

7027 silver badges12 bronze badges

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.

How do I go back to previous page?

Move between screens, webpages & apps Gesture navigation: Swipe from the left or right edge of the screen. 2-button navigation: Tap Back . 3-button navigation: Tap Back .

How do I go back to previous page on button click?

You can use the history. back() method to tell the browser to go back to the user's previous page. One way to use this JavaScript is to add it to the onclick event attribute of a button. Here, we create the button using a
element, containing an element of the button type.

What is history back ()?

The History. back() method causes the browser to move back one page in the session history. It has the same effect as calling history.go(-1) . If there is no previous page, this method call does nothing. This method is asynchronous.