Home Web Front-end HTML Tutorial Understanding browser history

Understanding browser history

Oct 11, 2016 pm 02:03 PM

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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

The Role of HTML: Structuring Web Content The Role of HTML: Structuring Web Content Apr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

See all articles