Web browsers have a back button that lets you go back to the previous page you just visited. If you want this functionality to be performed using a button in HTML you need to make use of history.back() function available in window object.
To demonstrate, let's create a HTML page with a button and call getPrevPage() function onClick of it. Create a function in head section script block.
<!DOCTYPE html>
<html>
<head>
<script>
function getPrevPage() {
//gets to the previous loaded page
window.history.back();
}
</script>
</head>
<body>
<input type="button" value="Back" onclick="getPrevPage()">
</body>
</html>
Similarly we have window.history.forward() that works just as the forward button on the web browsers. Lets create an example with both back and forward button implementation.
<!DOCTYPE html>
<html>
<head>
<title>Previous and Next Page button implementation using Javascript</title>
<script>
function getPrevPage() {
//gets to the previous loaded page
window.history.back();
}
function getNextPage() {
//gets to the next loaded page
window.history.forward();
}
</script>
</head>
<body>
<input type="button" value="Load Prev Page" onclick="getPrevPage()">
<input type="button" value="Load Next Page" onclick="getNextPage()">
</body>
</html>
- How to get UTC (GMT) time using JavaScript
- Submit html form on dropdown menu value selection or change using javascript
- How to detect Browser and Operating System Name and Version using JavaScript
- JavaScript : Get current page address
- [javaScript] Convert text case to lowercase
- How to check if a String contains substring or a word using javaScript
- Writing your first Hello, World! 🌍 JavaScript code Tutorial
- JavaScript: Check if variable is a number
- How to send email from JavaScript HTML using mailto
- Meaning of javascript:void(0) explained with example
- How to get query string in JavaScript HTML location.search
- Javascript convert text case from uppercase to lowercase
- Loading previous page using html button using JavaScript
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- How to get current URL Location using Javascript HTML
- Detect if Cookies are enabled using JavaScript
- Write javaScript code in Swedish using FikaScript
- Add Animated Scrolling to Html Page Title Script
- JavaScript: Convert an Image into Base64 String
- Remove items from JavaScript array
- Send Extra Data with Ajax Get or Post Request
- How to Print from JavaScript HTML using window.print
- Get Device Screen Width and Height using javaScript
- Examples: Convert String to int in JavaScript
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- How to list all tables using Java JDBC - Java
- Installing and using unzip Command to unzip a zip file using Terminal - Linux
- Xcode 13 - unknown error compiling for iOS 15.0 but module has a minimum deployment target of iOS 15.2 - iOS
- How to check your installed version of Git - Git
- How to open terminal on Mac to run commands - MacOS
- Fix Microsoft Teams Admin Center error - The Security zone setting isn't configured correctly - Teams
- How to change Chrome Spell Check from UK English to US English - Chrome
- Fix: TypeError: can only concatenate str (not int) to str in Python - Python