Use history to solve the problem that ajax does not support forward/backward/refresh. historyajax

Source: Internet
Author: User

Use history to solve the problem that ajax does not support forward/backward/refresh. historyajax

Preface:

Currently, both the front and back ends use ajax to interact with the front and back ends. However, ajax has a small disadvantage: it does not support the browser's "backward" and "Forward" keys.

However, now we can solve this small bug of ajax interaction requests through the histroy attribute of H5.

Event Description:

An event window. onpopstate is added to H5. When you click the two buttons, the event will be triggered. However, it is not enough to detect this event. You must pass some parameters, that is, you must know the pageIndex of the page when you return to the previous page. Through the pushState method of history, pushState (pageIndex) stores the pageIndex of the current page, and obtains the pageIndex when returning to this page.

Window. history. pushState description:

window.history.pushState(state, title, url);

State object:Is a JavaScript object, which is related to the new history entity created by the pushState () method. Used to store information about the entries you want to insert into history. The State object can be any Json string. Because firefox uses the user's hard disk to access the state object, the maximum storage space of this object is 640 kb. If the value is greater than this value, the pushState () method throws an exception.

Title:Firefox ignores this parameter now, although it may be used in the future. The safest way to use this function is to pass an empty string to prevent future modifications.

Url:The URL used to pass the new history object. The browser will not load the URL after the pushState () method is called. You may try to load the URL later. For example, after the user restarts the browser, the new url may not be an absolute path. If it is a relative path, it will be relative to the existing url. The new url must be in the same domain as the existing url, otherwise pushState () will throw an exception. This parameter is optional. If it is null, It will be set to the current url of the document.

Directly paste the Code:

/*** Created: Aaron. * address: http://www.cnblogs.com/aaron-pan/ * // Var pageIndex = window. history. state = null? 0: window. history. state. page; (function ($, window, undefined) {var loadData = {pageIndex: window. history. state = null? 1: window. history. state. page, // pageIndex: 0, init: function () {this. getData (this. pageIndex); this. nextPage () ;}, getData: function (pageIndex) {var that = this; $. ajax ({type: 'post', url :'. /data/getMovices '+ pageIndex + '. json', dataType: 'json', async: false, success: function (data) {that. renderDom (data) ;}}, renderDom: function (movies) {var bookHtml = "<table>" + "<tr>" + "<th> movie </th>" + "<th> Director </th>" +" <th> Release date </th> "+" </tr> "; for (var I = 0; I <movies. length; I ++) {bookHtml + = "<tr>" + "<td>" + movies [I]. moviesName + "</td>" + "<td> <a>" + movies [I]. moviesEditor + "</a> </td>" + "<td>" + movies [I]. times + "</td>" + "</tr>";} bookHtml + = "</table> "; bookHtml + = "<button> previous page </button>" + "<button class = 'nextpage'> next page </button>"; items ('body'{.html (bookHtml);}, nextPage: function () {var that = this; $ (document ). on ("Click ",". nextPage ", function () {that. pageIndex ++; that. getData (that. pageIndex); window. history. pushState ({page: that. pageIndex}, null, window. location. href); // return and refresh to the homepage window. history. replaceState ({page: that. pageIndex}, null, window. location. href) ;}},}; loadData. init (); window. addEventListener ("popstate", function (event) {var page = 0; if (event. state! = Null) {page = event. state. page; console. log ('page: '+ page);} console. log ('page: '+ page); loadData. getData (page); loadData. pageIndex = page;})} (jQuery, window, undefined );

You can view the running result by directly calling the js file on the html page.

Running result:

In this way, the functions of listening forward/back-end/Refresh can be achieved through ajax interaction.

Browser compatibility:

The above article uses history to solve the problem that ajax does not support forward, backward, or refresh. It is all the content that I have shared with you. I hope you can give us a reference and support more.

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.