JavaScript Back Method

JavaScript Back Button

The JavaScript “back()” method can reload content in the browser history list. A styled back button can be used to call the back method. The back method works in all major browsers.

The back method works well for static content such as HTML pages. It is possible to go several pages back through the history list.

This tutorial uses HTML5 but will work with older versions.

    Tools are required:

  • Text editor.
  • Folder for web server.
  • Browser to view output.

Go_Back_1.html File

<!--
   Go_Back_1.html
   
   Copyright 2013 edward <http://Ojambo.com>
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA.
   
   
-->

<!DOCTYPE html>
<html xml:lang="en" lang="en">

<head>
	<title>Ojambo.com Go Back</title>
	<meta charset="utf-8" />

	<script type="text/JavaScript">
		function goBack() {
				window.history.back()
		}	
	</script>
</head>

<body>
	<p>Ojambo.com Page One</p>
	<p><input type="button" value="Back Once" onclick="goBack()" /></p>
	<p><input type="button" value="Back Once" onclick="history.go(-1)" /></p>
	<p><input type="button" value="Back Twice" onclick="history.go(-2)" /></p>
	<a href="go-back2.html">Page 2</a>
</body>

</html>

Go_Back_2.html File

<!--
   Go_Back_2.html
   
   Copyright 2013 edward <http://Ojambo.com>
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA.
   
   
-->

<!DOCTYPE html>
<html xml:lang="en" lang="en">

<head>
	<title>Ojambo.com Go Back</title>
	<meta charset="utf-8" />

	<script type="text/JavaScript">
		function goBack() {
				window.history.back()
		}	
	</script>
</head>

<body>
	<p>Ojambo.com  Page Two</p>
	<p><input type="button" value="Back Once" onclick="goBack()" /></p>
	<p><input type="button" value="Back Once" onclick="history.go(-1)" /></p>
	<p><input type="button" value="Back Twice" onclick="history.go(-2)" /></p>
	<a href="go-back1.html">Page 1</a>
	<a href="go-back3.html">Page 3</a>
</body>

</html>

Go_Back_3.html File

<!--
   Go_Back_3.html
   
   Copyright 2013 edward <http://Ojambo.com>
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA.
   
   
-->

<!DOCTYPE html>
<html xml:lang="en" lang="en">

<head>
	<title>Ojambo.com Go Back</title>
	<meta charset="utf-8" />

	<script type="text/JavaScript">
		function goBack() {
				window.history.back()
		}	
	</script>
</head>

<body>
	<p>Ojambo.com Page Three</p>
	<p><input type="button" value="Back Once" onclick="goBack()" /></p>
	<p><input type="button" value="Back Once" onclick="history.go(-1)" /></p>
	<p><input type="button" value="Back Twice" onclick="history.go(-2)" /></p>
	<a href="go-back1.html">Page 1</a>
	<a href="go-back2.html">Page 2</a>
</body>

The first input tag utilizes the custom JavaScript function “goBack” which retrieves the last visited page. The second input tag calls the JavaScript method directly for going back one page. The third input tag goes back two pages; hence the minus two (-2).

Sometimes the content is dynamic or secured. A web developer can utlize an anchor tag in order to allow uses to specific pages. The anchor tag is a non-JavaScript way of navigating to specific pages.

How to Use:

    Open Browser

  • Navigate to other pages to build history.
  • Click on the input tags to go back.

Demonstration:

Ojambo.com JavaScript Back Method Tutorial

Image Missing
Ojambo.com JavaScript Back Button One

Image Missing
Ojambo.com JavaScript Back Button Two

Image Missing
Ojambo.com JavaScript Back Button Three

Conclusion:

Web application developers can create styled back buttons utilizing the JavaScript back method. Non-dynamic content works well with a back button.

An anchor tag is still the one of the best ways to allow visitors to navigate a site. A website cannot prevent visitors from using the back button.

    Recommendations:

  1. Do not assume visitors will not use their browser’s back button.
  2. Provide a navigation method for visitors.
  3. Use the JavaScript back method if the content is static.

About Edward

Edward is a software engineer, web developer, and author dedicated to helping people achieve their personal and professional goals through actionable advice and real-world tools.

As the author of impactful books including Learning JavaScript, Learning Python, Learning PHP and Mastering Blender Python API, Edward writes with a focus on personal growth, entrepreneurship, and practical success strategies. His work is designed to guide, motivate, and empower.

In addition to writing, Edward offers professional "full-stack development," "database design," "1-on-1 tutoring," "consulting sessions,", tailored to help you take the next step. Whether you are launching a business, developing a brand, or leveling up your mindset, Edward will be there to support you.

Edward also offers online courses designed to deepen your learning and accelerate your progress. Explore the programming on languages like JavaScript, Python and PHP to find the perfect fit for your journey.

📖 Explore His Books – Visit the Book Shop to grab your copies today.
💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.
🎓 Ready to Learn? – Check out his Online Courses to turn your ideas into results.