Home > Article > Web Front-end > Ajax fallback refresh page
This time I will bring you Ajax rollback to refresh the page. What are the precautions for Ajax rollback to refresh the page? The following is a practical case, let's take a look.
Ajax Introduction:
AJAX stands for "Asynchronous AJAX = Asynchronous JavaScript and XML (a subset of Standard Universal Markup Language). AJAX is a technology for creating fast, dynamic web pages. By exchanging a small amount of data with the server in the background, AJAX can enable asynchronous updates of web pages. This means that parts of a web page can be updated without reloading the entire page. Traditional web pages (not using AJAX) must reload the entire web page if the content needs to be updated.There is a problem
If you use a browser such as Firefox to access the RMS website, we may find that switching between pages is asynchronous through AJAX The request is implemented, and the URL of the page will not change at the same time. Although the rollback refresh can be implemented through the AJAX asynchronous request through the button on the page, the browser cannot support forward and backward. Every time after refreshing and backing, the page will return to The initial welcome page. AJAX can realize partial refresh of the page, can achieve very good data loading effect, and bring a very good experience to the user. However, AJAX cannot retain records in the browser's historical session. When you click on a page, AJAX various Data loading is very fast. For example, a list page can be turned through asynchronous loading. However, if the user accidentally refreshes the page, the page number will have to be calculated again. Once the user changes the session state (browser forward, backward, refresh) ), then AJAX will lose the relevant data.Traditional AJAX has the following problems:
1. The page content can be changed without refreshing, but the page URL cannot be changed2. Direct access to a certain module of the system through the URL cannot be supported. You must perform click operations. 3. The developer must put the first priority in rolling back and refreshing. This not only increases the workload for the developer, but also It is not in line with user habits4. Furthermore, the browser has introduced the onhashchange interface. Browsers that do not support it can only periodically determine whether the hash has changed5. However, this method is very harmful to search engines. UnfriendlyUsing technology
In order to solve the problems caused by traditional ajax, new APIs have been introduced in HTML5, namely: history.pushState, history.replaceStateYou can operate the browser history and change the URL of the current page through the pushState and replaceState interfaces. pushState is to add the specified URL to the browser history, and replaceState is to replace the current URL with the specified URL.history.pushState(state, title, url)Add the current URL and history.state to history, and add the new state and URL to the current. It will not cause the page to refresh. state: State information corresponding to the URL to be jumped to. title: Title (now ignored and not processed). url: URL address to jump to, cannot cross domain.
history.replaceState(state, title, url)The history.replaceState() operation is similar to history.pushState(), except that the replaceState() method modifies the current history entry rather than creating a new entry. state: State information corresponding to the URL to be jumped to. title: Title (now ignored and not processed). url: URL address to jump to, cannot cross domain. addEventListener(type, listener)
addEventListener is a function that listens to
events and processes the corresponding.
Solution
由于AJAX无刷新改变页面内容的,所以页面的URL始终是不变的,为了区分页面上的各个不同内容,首先需要重新定义一下各个页面的URL,因为RMS网站多使用$.post异步请求,我们可以用URL记录post请求的各个参数(请求地址、传递参数),当浏览器进行刷新、回退操作时,根据URL记录的信息自动发送post请求,进入对应页面,从而实现希望的功能。
定义URL语法:
已如下地址为例:
“http://localhost/rms_hold/index.php/Home/Index/loadHomePage#/rms_hold/index.php/Home/ResourceRequest/getRequestPage@apply_type=1&resource_name=ADM_BIZCARD!1”
“http://localhost/rms_hold/index.php/Home/Index/loadHomePage”是原先页面的URL,如果在问题解决之前在RMS网站上进行任何点按操作,网址一直不会有任何变动。现在我们使用“#”分割网址,“#”之后就是我们所记录的ajax请求“/rms_hold/index.php/Home/ResourceRequest/getRequestPage”是请求的地址,它由“#”与“@”分割,而在“@”与“!”之间的这是发向请求地址的各个参数,“apply_type=1”与“resource_name=ADM_BIZCARD”由“&”进行分割。
刷新、回退监听处理:
if (history.pushState) { window.addEventListener("popstate", function() { back_ajax_mod_url(); back_ajax_post(); if(location.href.indexOf("#")==-1){ window.location.reload(); } }); back_ajax_mod_url(); back_ajax_post(); }
如以上代码所示,window对象上提供了onpopstate事件,可以使用addEventListener方法监听onpopstate事件,每当URL因为浏览器回退时都会对得到的URL在back_ajax_mod_url()与back_ajax_post()函数中进行解析、处理,而当浏览器刷新时,根据history.pushState的返回值不空,依然会对得到的URL在back_ajax_mod_url()与back_ajax_post()函数中进行解析、处理。
对外接口:
function back_ajax_mod_url(){ var url_ajax=ajaxback_url.pop(); var title ="Home | UniqueSoft RMS"; if(url_ajax){ history.pushState({ title: title }, title,location.href.split("#")[0] + "#"+ url_ajax); } }
介绍一下back_ajax_mod_url()函数,它与数组ajaxback_url组成对外接口,ajaxback_url是一个全局数组,用来存放需要加入到history中的URL,然后由back_ajax_mod_url()函数在无页面刷新的情况下将当前URL和history.state加入到history中。
$("#reportTable tbody").on("click", "trtd img[alt = 'Detail']", function() { var id = $(this).attr("business_leave_id"); $.post("MODULE/ReportCenter/getReportDetailPage",{ "report_name": "ADM_TRAVEL_REP", "item_id": id, }, function(data) { ajaxback_url.push("MODULE/ReportCenter/getReportDetailPage"+ "@" + "item_id=" + id + "&" +"report_name=ADM_TRAVEL_REP"); $("#container").html(data); back_ajax_mod_url(); }); });
以上函数是RMS系统里的一个AJAX异步请求事件,会造成页面无刷新变化,加粗部分就是我们提供的对外接口,使用该接口后在history中会产生一条新的URL用来记录达到该页面的post方法。
URL解析处理器:
如下面函数所示back_ajax_post()为RMS系统的URL解析处理器,根据之前提到的URL语法,读出页面上改变内容的AJAX请求,并且自动发送AJAX请求,获取需要的页面
function back_ajax_post() { if (location.href.indexOf("#")!= -1) { var post_href =location.href.split("#")[1]; if (location.href.indexOf("@")!= -1) { var post_url =post_href.split("@")[0]; var post_params =post_href.split("@")[1]; if(post_params.indexOf("!") != -1) { var post_page_index =post_params.split("!")[1]; post_params =post_params.split("!")[0]; }; } else { var post_url = post_href; var post_params = ""; var post_page_index = ""; } var get_resource_href =location.href; if(get_resource_href.indexOf("!") != -1) { get_resource_href =get_resource_href.split("!")[0]; }; if(get_resource_href.indexOf("resource_name=") != -1) { var has_resource_name =get_resource_href.split("resource_name=")[1]; var siderbar_index =has_resource_name; } else if(get_resource_href.indexOf("report_name=") != -1) { var has_resource_name =get_resource_href.split("report_name=")[1]; var siderbar_index =has_resource_name.split("_REP")[0]; }; if (!post_page_index ||$("#personalInfo").length <= 0) { if (!post_url) { window.location.href ="MODULE"; } $.ajax({ type: "post", url: post_url, data: post_params, success: function(res){ $('#pageContainer').html(res); if(post_page_index) { location.href= location.href.split("!")[0] + "!1"; } else { location.href= location.href.split("!")[0]; }; }, error: function(res) { window.location.href = "MODULE"; }, }); } //for request page next&back if (post_page_index) { var previous_index =$(".navbar,.steps .navbar-innerul.row-fluid").find("li.active").find(".number").text(); var differ =post_page_index - previous_index; lock_for_req_back_next =1; if (differ > 0) { for (var i = 0; i <differ; a="" bar="" differ="-differ;" else="" for="" i="0;" if="" li="" lock_for_req_back_next="0;" resource_name="$(this).attr("href").split("resource_name=")[1];" side="" siderbar_index="=" ul.page-sidebar-menuli="" ul.sub-menu="" var=""> span.arrow').addClass('open'); $(this).parents('.sub-menu').show(); }); $(this).parent('li').parents('li').addClass('active open'); return false; } else { $('.sub-menu').hide(); } }); $("ul.page-sidebar-menuli").not(".open").find("ul").hide(); } } </differ;>
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Ajax fallback refresh page. For more information, please follow other related articles on the PHP Chinese website!