search
HomeWeb Front-endHTML TutorialUnderstanding browser history

This is a basic article that talks about some related content of history stack management in browsers. The reason for writing this is that I recently wanted to study pushState to see what problems would be encountered when using it to implement SPA. PushState ultimately affects the content in the browser history stack, so I spent some time thinking about it. How browsers manage the history stack. Because during the research process, I discovered some important points that I had never noticed before, so I recorded them.

Demo address: http://liuyunzhuge.github.io/blog/history/demo1.html

This demo is used for related tests mentioned later in this article. If you are also interested, it is recommended that you open this demo in a new tab every time you want to test a new problem, instead of opening it from an already opened one. Open the tab of the web page; because the tab of the web page has been opened, its history stack already contains records of previously visited web pages, so it will affect the results of the problem you want to test.

The browser will record the web pages visited in the same window (tab). No matter we change the web page in any of the following ways, the browser will record the changed web page so that it can be used through the browser's forward and back buttons. , can quickly switch to web pages that have been visited:
1) Enter the web page address directly in the address bar;
2) Click on the hyperlink in the web page to jump to other web pages; but it cannot be a link that opens in a new window ;
3) Jump to other web pages by changing location.href through a script;
4) Jump to other web pages by submitting a form; but it cannot be a form submitted to a new window.
In short, as long as the web page jumps within the same window, the browser will record it. Except for refresh, the length attribute of the history object can view the total number of historical records stored in the current window. In the previous demo page, I printed this attribute on the page. This attribute will only change when the web page changes; while refreshing the web page This property will not be changed.

The browser has a data structure to store the history of web pages. I call it a history stack because its structure is somewhat similar to the way a stack is used.

Operation test one: If you copy the previous demo address, and then follow the following steps in the chrome browser:

Open a new tab; enter demo1.html; click demo2.html; click demo3.html; click demo4.html; click demo3.html; click demo2.html; click demo1.html.

The browser will store the above access records in a way similar to the figure below:

Understanding browser history

Since current browsers are in multi-tab mode, when you open a tab, even if you do not visit a specific web page, the browser will create a BOM object for this tab, such as the history object, and then add the new option The blank page of the card is used as the first record in the history. So you can see 8 records in the last column in the picture above, which is the same as the number displayed on the demo page:

Understanding browser history

Along with the history stack, the browser also has an access pointer to indicate the position of the current web page in the history stack. By default, when we change the web page address through the methods listed above, the new page will be pushed to the top of the history stack and the pointer will be pointed to the latest web page, as shown in the figure above. Every time the page is changed, the pointer of the current page always points to the record at the top of the history stack; when we use the browser's forward and backward functions (including buttons, shortcuts, right-click menus, etc.) or the go provided by history The /back/forward methods will not change the contents of the history stack, but will only move the pointer:

1) The forward function/go(1)/forward just moves the pointer up by 1 position;

2) The backward function/go(-1)/forward just moves the pointer down by 1 position;

3) go(n) moves the pointer up n positions; go(-n) moves the pointer down n positions.

The browser finds the web page in the history stack and displays it based on the moved pointer position. If you continue to perform the operation to test the result of , continue to do the following operations.

Operation test 2: Click the browser back button 7 times.

At this time, the storage status of the history stack of the browser becomes the following state:

Understanding browser history

Although history.go(n) and history.go(-n) can move the pointer to any position, when the position to be moved exceeds the position range of the history stack, the pointer will not move. So in the results of Operation Test 2, calling history.go(-100) and history.go(100) will not work.

There are two other situations that will change the contents of the history stack.

Operation Test 3: If we follow the results of Operation Test 2 and click the forward button three times, the browser’s history stack will enter the following state:

Understanding browser history

At this time, since neither operation test 2 nor operation test 3 changed the content of the history stack, if correct, the history statistics on the page should still be 8:

Understanding browser history

Operation test four: Next, we do the following two steps: click demo2.html, click demo3.html. At this time, the history statistics on the page become:

Understanding browser history

history.length has changed, indicating that the contents of the history stack have also changed. But why does it become 6 instead of 10 (8+2)? Take a look at the state of the browser history stack at this point:

Understanding browser history

When the browser pushes a new record into the history stack, it pushes it directly behind the current pointer. If there are other record items behind the current pointer, they will be discarded. This makes it easy to understand why the total number of historical records after operation test four is only 6.

Another important point in browser management of history records is that there is a limit on the total number of history stacks stored, both Chrome and Firefox are 50. When the storage amount of the history stack exceeds this limit, the storage of history records will be stored in a rolling manner, that is, new records will be pushed to the top of the stack, and the bottom records will be removed from the bottom of the stack. . By continuously switching and clicking demo1, demo2, demo3, and demo4 on the demo page, when a certain number of times is reached, the statistical information printed on the page will no longer change, which means that the historical record limit has been reached. However, in IE11, I clicked to more than 100 and found that it was still changing, indicating that IE's restrictions may or may not be higher. .

This article records some content about browser history management. There may be some imperfections in the analysis. Your criticism and corrections are welcome. I hope the above content will be helpful to friends in need to deepen their understanding of history and pushState. Thank you for reading:)

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
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.