HTML5 History API implements refresh-free jump_html5 tutorial skills
Once when I was surfing the Internet, I found that the login and registration animation effects were very gorgeous, but what shocked me was that the page could jump without refreshing (it has been revised, you can click here to see this effect: GitHub or FM), I reviewed the front-end knowledge I had learned, and it seemed that there was no technology that could achieve this. So I searched on Baidu and discovered that this was originally achieved by using the History API in HTML5, but it has never been put to use. It was not until the blog was revised that this technology was applied.
In HTML5,
1. Added the ability to add items in the browser history through JS.
2. Display and change the URL in the browser address bar without refreshing the page.
3. Added an event that is triggered when the user clicks the browser's back button.
Through the above three points, you can dynamically change the URL in the browser address bar and dynamically display the page content without refreshing the page.
For example: When the content of page A and page B are different, before HTML5, if you switch from page A to page B, you need to switch from page A to page B in the browser, or in other words, if you need If you want to use the back button function, you can add #XXXX to the URL address to achieve the back function. So now in HTML5, you can implement the following processing through the History API:
1. Request the B data in the page by sending an AJAX request on page A.
2. Load the corresponding information to the corresponding location through JS in page A.
3. Use the History API to switch from the URL address of page A to the URL address of page B in the browser's address bar without refreshing the page.
History API in HTML4
Attributes
1.length The number of history items. The history that JavaScript can manage is limited to the range that can be reached using the browser's "forward" and "back" keys. This attribute returns the sum of the addresses contained under the "forward" and "backward" buttons.
Methods
1.back() Back, which is equivalent to pressing the "Back" key.
2.forward() forward, which is equivalent to pressing the "forward" key.
3.go() Usage: history.go(x); Go to a specified address within the history range. If x < 0, go back x addresses, if x > 0, go forward x addresses, and if x == 0, refresh the now open web page. history.go(0) is equivalent to location.reload().
History API in HTML5
1. history.pushState(data, title [, url]): Add a record to the top of the history stack; data will be in When the onpopstate event is triggered, it is passed as a parameter; title is the page title, and all current browsers will ignore this parameter; url is the page address, optional, and the default is the current page address.
2. history.replaceState(data, title [, url]): Change the current history record, the parameters are the same as above.
3. history.state: used to store the data of the above method. Different browsers have different read and write permissions.
4. popstate event: This event is triggered when the user clicks the browser's back or forward button. In the event processing function, read the state attribute value of the event object that triggered the event. This attribute value is the first parameter value used when executing the pushState method, which stores the synchronously saved value when adding a record to the browser history. object.
So far, IE10, firefox4 and above, Chrome8 and above, Safari5, Opera11 and above browsers support the History API in HTML5.
HTML:
JS:
/**
* HTML history and ajax
*/
$(function(){
var ajax,
currentState;
$('.container li').unbind().bind('click',function(e){
e.preventDefault();
var target = e.target,
url = $(target).attr('href');
!$(this).hasClass('current') && $(this).addClass('current').siblings().removeClass("current");
if(ajax == undefined) {
currentState = {
url: document.location.href,
title: document.title,
html: $('.content').html()
};
}
ajax = $.ajax({
type:'POST',
url: url,
dataType:'json',
success: function(data){
var html = '';
if(data.length > ) {
for(var i = , ilist = data.length; i < ilist; i ) {
html = '
'
'
}
$('.content').html(html);
}
var state = {
url: url,
title: document.title,
html: $('.content').html()
};
history.pushState(state,null,url);
}
});
});
window.addEventListener('popstate',function(event){
if(ajax == null){
return;
}else if(event && event.state){
document.title = event.state.title;
$('.content').html(event.state.html);
}else{
document.title = currentState.title;
$('.content').html(currentState.html);
}
});
});

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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.

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

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

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

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

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

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