History go() method : The history object has the go() method which is used to load page from the browser history. You can choose specific url or index of the previous or next browsed page.
Syntax of the go() method :
history.go(number|URL)
number|URL : Required. This parameter can have numeric value or a string. The numeric value refer the index of the URL in browser history, for example if you want to go 2 pages back then you can simply give go(-2) and to go next go(2). And you can also pass a URL string if find any matches in history then it will open that page.
Example of history go() method : Here is the sample code to show how you can load previous and next page using JavaScript history.go() function.
Sample1.html
<!DOCTYPE html>
<html>
<body>
<p>To go to the 2 pages back click the button "Go 2 pages back".</p>
<button onclick="goToBack()">Go 2 pages back</button>
<script>
function goToBack() {
window.history.go(-2);
}
</script>
</body>
</html>
This sample code will take you to the 2 pages back from the history.
Sample2.html
<!DOCTYPE html>
<html>
<body>
<p>To go to the next page click the button "Go To Next Page from URL history".</p>
<button onclick="goToNext()">Go To Next Page from URL history</button>
<script>
function goToNext() {
window.history.go(1);
}
</script>
</body>
</html>
0 Comment(s)