JavaScript history go() method

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:

  1. 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);
  1. To navigate the browser forward two pages in the history, you can use the following code:
history.go(2);
  1. 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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: