Javascript History go function can perform both the functions as javascript history back and forward function can do. Javascript history go function accepts integer value or string value to perform the specific action according to the parameter passed. -1 value as a parameter passed to the history go function performs the history.back action and 1 as history.forward. Javascript history go function also accepts the string type value that can be used to pass the URL of the previous or next URL from the browser history. But URL string parameter is rarely used.
Syntax for Javascript History Go Function
history.go(number|[URL])
or
window.history.go(number|[URL])
history.go function of javascript can also be accessed using window object. You can pass -1 or 1 to the go function of history object or you can pass the URL of the previously visited page stored in the web browser history.
Examples of Javascript History Go Function
<html>
<head>
<title>Javascript History Object Go Function</title>
</head>
<body>
<input
id="btnGo"
type="button"
value="Go"
onclick="history.go(1);" />
<input
id="btnGo"
type="button"
value="Go"
onclick="history.go(-1);" />
</body>
</html>
Above example shows the use of history.go function with two types of number values as -1 or 1.
history.go(-1) loads the previous URL from the history of web browser and history.go(1) loads the next URL saved in the web browser history collection.
Continue to next tutorial: Javascript Window History Object to get information about all JavaScript history object methods.