HTML5 API-Refresh update Address history. pushstate/replacestate Method

Source: Internet
Author: User

Javascript advancedProgramAs mentioned in design (version 2), location. Path/query in Bom ...... (Window. Location) after being changed through JavaScript, the browser will refresh the URL you changed (location means location ..)

Since JavaScript MVC began to become popular, the method of modifying the URL through refresh made people feel annoyed. However, HTML5 creates an API that can be used to modify the URL without refreshing the browser, that is, the history API.

Those familiar with Javascript development are certainly familiar with history. The most classic method is go. The first transmission parameter of the integer type allows the browser to arrive before or after the current page, pages that have been browsed. Of course, this is also implemented by refresh.

Now, two methods are added to the history API, pushstate and replacestate. In fact, they are used in the same way. You cannot see the differences between them in Mozilla documents. Haha.

The usage is as follows:

VaR state = {// you can give the browser a State object to prepare for the subsequent stateevent.

Title: "HTML 5 history API simple demo ",

URL: "yourpage"

};

History. pushstate (state, "HTML 5 history API simple demo", "yourpage ");

It's easy, so replacestate is also used:

VaR state = {// you can give the browser a State object to prepare for the subsequent stateevent.

Title: "HTML 5 history API simple demo ",

URL: "yourpage "};

History. replacestate (state, "HTML 5 history API simple demo", "yourpage ");

State event

Since there are no methods to refresh and change the URL, of course there is time to respond to this change.

Well, that's right. There is a popstate event, and the input handler function has a parameter, that is, the first parameter in pushstate, a State obj. Developers can use this state OBJ to identify behavior. DetailsCodeAs follows:

VaR state = {// you can give the browser a State object to prepare for the subsequent stateevent.

Title: "HTML 5 history API simple demo ",

URL: "yourpage"

};

History. pushstate (state, "HTML 5 history API simple demo", "yourpage ");

Window. onpopstate = function (e) {document. Title = E. State. Title ;}

You can also do this:

VaR state = {// you can give the browser a State object to prepare for the subsequent stateevent.

Action: "page ",

Title: "HTML 5 history API simple demo ",

URL: "yourpage"

};

History. pushstate (state, "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;

}

}

The above is a navigation handler (navigation handler) combined with pushstate and Ajax. In fact, there is already a good jquery plug-in, jquery-pjax (pushstate and Ajax ).

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.