Home Web Front-end H5 Tutorial HTML5 actual combat and analysis of history management (history object)

HTML5 actual combat and analysis of history management (history object)

Feb 13, 2017 pm 01:56 PM

HTML5 has newly added the management of history, and updated the history object to make it more convenient to manage historical status. In modern web applications, users can switch history pages through the "forward" and "back" buttons. This allows some new pages that are not opened in new pages to move forward and back freely, improving user experience.

Through the haschange event, you can know when the parameters of the URL have changed, that is, when you should react. Through the state management API, the browser URL can be changed without loading a new page. So you need to use the history.pushState() method. The history.pushState() method receives three parameters: 1. Content to be stored 2. Title (usually an empty string) 3. Address (optional). A small example is as follows


 JavaScript code##

history.pushState({name: "menglong"}, '', "li.html");
Copy after login

Executed history.pushState () method, the new status information will be added to the historical status stack, and the browser address bar will become the new relative URL. However, the browser does not send a request to the server, and even if the new location.href is checked after the historical status changes, the same address as in the address bar will be returned. In addition, the second parameter is not currently implemented by browsers, so you can just pass in an empty string, or a short title. The first parameter should provide as much information as possible to initialize the page state.

Because the history.pushState() method creates a new historical state, you will find that the "Back" button can also be used. Pressing the "Back" button will trigger the popstate event of the window object. The event object of the Popstate event has a state property, which contains the state object originally passed to pushState() as the first parameter. A small example is as follows


 

JavaScript code##

window.addEventListener('popstate',function(ev){
	var state = event.state;
	if(state){ // 当第一个页面加载的时候state为空
		processState(state)
	}
}, false);
Copy after login
Got this status Object, you must reset the page to the state represented by the data in the state object (because the browser will not do this for you automatically). Remember, the first page loaded by the browser has no state, so when "back" Aniu returns to the first page loaded by the browser, the event.state value is null.

To update the current historical state, you can call replaceState(), and the parameters passed in are the same as the first two parameters of the pushState() method. Calling replaceState() will not create a new state in the historical state stack, it will only overwrite the current state. A small example is as follows

 

JavaScript code

history.replaveState({name: "meng"}, "meng1234");
Copy after login
When using the historical state management mechanism of HTML5, you need to make sure to use Every "fake" URL created by pushState() has a real, actual URL corresponding to it on the web server. Otherwise, the stand-alone "Refresh" button will result in a 404 error.

Browsers that support HTML5 history state management include Chrome, Safari 5+, Firefox 4+ and Opera 11.5+. In Safari and Chrome, the state object passed to pushState() or replaceState() cannot contain DOM elements. Firefox supports including DOM elements in state objects. Opera also supports a history.state property, which returns a state object for the current state. The following is time for small examples. Only by combining small examples can we better understand the history management in HTML5.


 

Add href value to achieve history management 

HTML code

<input type="button" value="35选7" id="input1" />
<p id="p1"></p>
Copy after login
Copy after login
 

JavaScript code

//onhashchange : 事件 : 当hash值改变的时候触发的事件

//hash改变就会出现就会出现历史管理

window.onload = function(){
	var oInput = document.getElementById(&#39;input1&#39;);
	var op = document.getElementById(&#39;p1&#39;);
	
	var obj = {};
	
	oInput.onclick = function(){
	
		var number = randomNum(35,7);
		
		op.innerHTML = number;
		
		var oRN = Math.random();
		
		obj[oRN] = number;
		
		window.location.hash = oRN;
	
	};
	
	
	window.onhashchange = function(){
	
		var number = obj[window.location.hash.substring(1)] || &#39;&#39;;
		
		op.innerHTML = number;
	
	};
	
	
	function randomNum(alls,now){
	
		var arr = [];
		var newArr = [];
		
		for(var i=1;i<=alls;i++){
			arr.push(i);
		}
		
		
		for(var i=0;i<now;i++){
			newArr.push( arr.splice( Math.floor(Math.random()*arr.length) ,1 ) );
		}
		
		return newArr;
	
	}
	
};
Copy after login
 

Passed History object in HTML5 implements history management## 

HTML code

<input type="button" value="35选7" id="input1" />
<p id="p1"></p>
Copy after login
Copy after login
 

JavaScript code

//pushState : 三个参数:1.要存的内容 2.标题(一般写个空的字符串) 3.地址(可选)

//history.pushState({name: "menglong"}, &#39;&#39;, "li.html");

window.onload = function(){
	var oInput = document.getElementById('input1');
	var op = document.getElementById('p1');
	
	var iNow = 0;
	
	oInput.onclick = function(){
	
		var number = randomNum(35,7);
		
		op.innerHTML = number;

		history.pushState(number,'',iNow++);
	
	};
	
	
	window.onpopstate = function(ev){  //历史管理改变,就会触发
		
		var number = ev.state || '';

		op.innerHTML = number;
		
	};
	
	
	function randomNum(alls,now){
	
		var arr = [];
		var newArr = [];
		
		for(var i=1;i<=alls;i++){
			arr.push(i);
		}
		
		
		for(var i=0;i
Copy after login
The above is the content of HTML5 actual combat and analysis of history management (history object). For more related content, please pay attention to the PHP Chinese website (www .php.cn)!

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 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
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
24
Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

See all articles