"Go" HTML5 api--no Refresh Update address History.pushstate/replacestate method

Source: Internet
Author: User

(window.location) After the change through JavaScript, the browser will be refreshed to reach your changed URL (location is the meaning of the site). )

And after JavaScript MVC became popular, the way to modify URLs by refreshing them was annoying. In HTML5, however, an API is developed that allows you to modify the URL in a way that does not refresh the browser, which is the history API.

Familiar with the development of JavaScript, the history is certainly not unfamiliar, the most classic method is go, through the first type is an integer transmission parameters, you can make the browser to reach the current page before or after the page has been browsed. Of course, this is also to be refreshed to achieve.

Now the history API added two methods, namely Pushstate and replacestate, in fact, the use of the same, look at the Mozilla document also did not see how different they are, haha.

Use the following:

var state = {//This can be a state object that you want to give the browser, ready for the stateevent behind.

Title: "HTML 5 History API Simple Demo",

URL: "Yourpage"

};

History.pushstate (state, "the HTML 5 history API Simple Demo", "Yourpage");

It is simple, then replacestate is the same usage:

var state = {//This can be a state object that you want to give the browser, ready for the stateevent behind.

Title: "HTML 5 History API Simple Demo",

URL: "Yourpage"};

History.replacestate (state, "the HTML 5 history API Simple Demo", "Yourpage");

State Event

Since there is no way to refresh the URL change, of course, also have to respond to this change of time.

Well, that's right. There is a Popstate event, and the incoming handler function has a parameter, which is the first parameter in the Pushstate, a state obj. Developers can use this state obj to identify behavior. The detailed code is as follows:

var state = {//This can be a state object that you want to give the browser, ready for the stateevent behind.

Title: "HTML 5 History API Simple Demo",

URL: "Yourpage"

};

History.pushstate (state, "the HTML 5 history API Simple Demo", "Yourpage");

Window.onpopstate = function (e) {document.title = E.state.title;}

Of course, you can do this:

var state = {//This can be a state object that you want to give the browser, ready for the stateevent behind.

Action: "Page",

Title: "HTML 5 History API Simple Demo",

URL: "Yourpage"

};

History.pushstate (state, "the HTML 5 history API Simple Demo", "Yourpage");

Window.onpopstate = function (e) {

Switch (e.state.action) {

Case "Home":

Document.title = "HOME ...";

$.get ("index.php"). Done (function (data) {$ ("#wrapper"). HTML (data);});

Break

Case "page":

Document.title = E.state.title;

$.get (E.state.url). Done (function (data) {$ ("#wrapper"). HTML (data);});

Break

}

}

Above this is a combination of pushstate and Ajax to do a navigation corresponding device (Navigation Handler), actually already has a good jQuery plugin, Jquery-pjax (Pushstate and Ajax).

"Go" HTML5 api--no Refresh Update address History.pushstate/replacestate method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.