Use Ajax to implement no refresh changes page content and address bar Url_ajax related

Source: Internet
Author: User
Tags hash

When visiting Google Plus, which is now hot, careful users may find that the clicks between pages are asynchronously requested by Ajax, and the URL of the page changes. and can support the browser's forward and backward very well. Can not help but ask, what is such a powerful function?

HTML5 quotes the new API, is history.pushstate and history.replacestate, is through this interface to do without refreshing change the page URL.

The difference from the traditional Ajax

Traditional Ajax has the following problems:

Although Ajax can change page content without refreshing, it cannot change the page URL

Second, for better accessibility, change the hash of the URL after the content has changed. But the hash is not a good way to deal with the browser's forward, backward and other issues

Some browsers introduce Onhashchange interfaces, and browsers that do not support it can only be timed to determine whether the hash has changed

Again, the use of Ajax is very unfriendly to search engines, where spiders often crawl into empty areas.

In order to solve the problems caused by traditional Ajax, HTML5 introduced a new API, namely: History.pushstate, History.replacestate

You can manipulate browser history through the Pushstate and Replacestate interfaces, and change the URL of the current page.

Pushstate adds the specified URL to the browser history, Replacestate replaces the specified URL with the current URL.

How to call

Copy Code code as follows:

var state = {
Title:title,
Url:options.url,
Otherkey:othervalue
};
Window.history.pushState (state, Document.title, URL);

In addition to the title and URL, the state object can add additional data, such as: also want to save some of the configuration to send Ajax.

Replacestate and Pushstate are similar and do not need to be explained more.

How to respond to the browser's forward and backward operations

The Onpopstate event is provided on the Window object, and the state object passed above becomes the child object of the event, so that the title and URL of the store is available.

Copy Code code as follows:

Window.addeventlistener (' Popstate ', function (e) {
if (history.state) {
var state = E.state;
Do something (State.url, state.title);
}
}, False);

This can be combined with Ajax and Pushstate perfect for no refresh browsing.

Some restrictions

1, can not cross the domain, this is inevitable. Quote: "If JavaScript can cross a domain, then he can reverse the sky!" ”

2. Although the state object can store many custom properties, the value cannot be an object.

Some processing of the corresponding back end

In this mode, in addition to the current use of Ajax can not refresh browsing, but also to ensure the direct request for changes in the URL can also be normal browsing, so the backend to these processing.

1, the combination of pushstate Ajax can send a special header, such as: setRequestHeader (' Pjax ', ' true ').

2, the back-end to get the header with Pjax=true, the page will not be common parts of the output. For example: PHP can be judged by the following

Copy Code code as follows:

function Is_pjax () {
Return array_key_exists (' Http_x_pjax ', $_server) && $_server[' http_x_pjax '] = = = ' true ';
}

Although the interface is only Pushstate, Replacestate, Onpopstate, but in the use of time to do a lot of processing.

The above is the entire content of this article, I hope you can enjoy.

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.