Thursday, June 13, 2013

How to navigate forward and back to the pages using JavaScript

Follow the steps to navigate forward and back to the pages using JavaScript.
<html>
<head>
<script language='JavaScript' type='text/javascript'>
function goBack() {
     window.history.back();
}
</script>
</head>
<body>
<input type='button' name='back' value='Go Back' title='Click here to go back' onClick='goBack()'>
</body>
</html>
When the user click on the button it will go back to last visited page which means previous page.
We can write this code in another way.
<script language='JavaScript' type='text/javascript'>
function goBack() {
     window.history.go(-1);
}
</script>
This script is also works same as the above script. This is the same as the back button functionality of the browser. Here '-1' represents the previous page.

And we can use this script to load the next URL in in the history list.
<html>
<head>
<script language='JavaScript' type='text/javascript'>
function goForward() {
     window.history.forward();
}
</script>
</head>
<body>
<input type='button' name='forward' value='Go Forward' title='Click here to go Forward' onClick='goForward()'>
</body>
</html>
This is the same as the forward button functionality of the browser.

Thank you...

No comments:

Post a Comment