The JavaScript history go() method is used to navigate the browser to a specific page in the history. It takes a single argument: the number of pages to move in the history (negative numbers move back and positive numbers move forward). It does not return a value.
Here are some examples of how the go() method can be used in JavaScript code:
- To navigate the browser back one page in the history, you can use the following code:
// navigate the browser back one page in the history
history.go(-1);
- To navigate the browser forward two pages in the history, you can use the following code:
history.go(2);
- To navigate the browser to a specific page in the history, you can use the following code:
// define the index of the page in the history
const pageIndex = 10;
// navigate the browser to the specified page in the history
history.go(pageIndex);
Overall, the go() method provides a convenient way to navigate the browser to a specific page in the history. It can be useful for tasks such as implementing a “back” or “forward” button or link, or for other operations that require navigating the browser to a specific page in the history.
Leave a Reply