Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is JavaScript Window History and how to use it

Shulou Source: shulou.com Published: 2022-05-31 10:35:32 09月23日 Update

This article introduces the relevant knowledge of "what is JavaScript Window History and how to use it". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Preface

Window.history objects can be written without the window window prefix. To protect the privacy of users, restricted JavaScript can access this object.

History.back ()-same as clicking the back button in the browser.

History.forward ()-same as clicking the forward button in the browser.

2. History object

The window.history object contains the browser session history, a list of all pages accessed in the current frame or window, and window.history can write objects without window prefixes.

However, in order to protect the privacy of users, there are some restrictions on how JavaScript accesses this object. The window.history object contains browser history.

3. Get the number of pages visited

This history.length property returns the number of pages in the current window in the browser session history, as well as the currently loaded pages.

Example

Var result = history.length; / / returns the size of the current session history complete code: project

Number of URL in the history list:

Note: this example opens in the new framework and will be treated as a new "session":

Var result = history.length; document.getElementById ("para") [xss_clean] = result

You can use this property to find out how many pages the user visited during the current browser session.

1. Return to the previous page

The history.back () method loads the previous URL in the history list.

This is the same as clicking the back button in the browser.

Project advance

Note: clicking the forward button here will not do anything because there is no next URL in the history list:

The above code displays the following output (this example will not work if the next page does not exist in the history list):

3. Go to a specific page

You can use the history.go () method to load a specific page from the session history.

This method takes an integer as a parameter, a negative integer moves backward in the history, and a positive integer moves backward in the history.

Example

Project

Tags: History browser browsing object button page same code project output integer method user face content prefix property more frame knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MariaDB Docker Microsoft MySQL