Home  >  Article  >  Web Front-end  >  What is the JavaScript history object?

What is the JavaScript history object?

青灯夜游
青灯夜游Original
2018-12-13 18:04:092887browse

The history object of JavaScript is part of the window object and represents an array collection of URLs visited by the user; it is used to save the user's online history, starting from the moment the window is opened.

What is the JavaScript history object?

This article introduces the history object of JavaScript to everyone, so that everyone can have a simple understanding of the History object and know the properties and methods of the History object. I hope it will be helpful to you. .

Attributes of the History object

The History object has only one attribute, that is: length, used to return historical records The number of URLs in the table. Example:

history.length // 如果在当前窗口先后访问了三个网站,有了三个URL,就会返回3

Methods of the History object

The History object has three methods, which are:

forward(): Loads the previous URL in the history list.

back(): Load the next URL in the history list.

go(): Load a specific page in the history list, you can accept an integer as a parameter, for example:

history.go(1)
/*相当于*/
history.forward()

history.go(-1)
/*相当于*/
history.back()

history.history.go(0)  /*相当于刷新当前页面*/

Example of the History object method:

History object method can be used in the "return to previous page" link

1.html

<div id="demo">
	<a href="file:///D:/ksdler/Sublime%20Text/demo/2.html">2.html</a><br>
	<a href="#" id="backLink">返回上一页</a><br>

</div>
<script>  
document.getElementById(&#39;backLink&#39;).onclick = function () {
  window.history.back();
}
</script>

2.html

<div id="demo">
    <a href="file:///D:/ksdler/Sublime%20Text/demo/1.html">1.html</a><br>
    <a href="#" id="backLink">返回上一页</a><br>
</div>
<script>  
document.getElementById(&#39;backLink&#39;).onclick = function () {
  window.history.back();
}
</script>

Rendering :

What is the JavaScript history object?

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of What is the JavaScript history object?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn