您好,欢迎访问一九零五行业门户网

location.hash保存页面状态的技巧_javascript技巧

hash 属性是一个可读可写的字符串,该字符串是 url 的锚部分(从 # 号开始的部分)。
语法
location.hash
在我们的项目中,有大量ajax查询表单+结果列表的页面,由于查询结果是ajax返回的,当用户点击列表的某一项进入详情页之后,再点击浏览器回退按钮返回ajax查询页面,这时大家都知道查询页面的表单和结果都回到了默认状态。
如果每次返回页面都要重新输入查询条件,或有甚者还得转到列表的第几页,那这种体验用户真的要抓狂了。
在我们的项目中,写了一个很简单的javascript基类来处理location.hash从而保存页面状态,今天在此就分享给大家。
(本文的内容可能对于javascript初学者来讲有点难度,因为涉及到js面向对象的知识,如定义类、继承、虚方法、反射等)
先看看我们的需求
我们的项目是一个基于微信的h5任务管理系统,要完成的页面原型如下图所示:
需求应该都很清晰,就是点击查询表单,用ajax返回查询结果,然后点击列表中的某一个任务进入任务详情页。由于管理员(项目经理)通常会一次处理多个任务,所以就会不断在任务详情页跟查询列表页切换,这时如果按返回键不能保存查询页面状态的话,那每次返回查询页面都要重新输入查询条件,这样的体验肯定是不能忍受的。
所以,我们需要想办法将页面状态保存下来,以便用户按回退键的时候,查询条件和结果都还在。
解决思路
保存页面状态的思路有很多啦,但是我们觉得用location.hash应该是最好的方法。
思路如下:
1.用户输入查询条件并点击确定后,我们将查询条件序列化成一个字符串,并通过“#”将查询条件加到url后面得到一个新的url,然后调用location.replace(新的url)修改浏览器地址栏中的地址。
2.当用户按回退键回退到查询页面时,也可以说是页面加载时,将location.hash反序列化成查询条件,然后将查询条件更新到查询表单并执行查询即可。
思路很简单,关键的地方就是location.replace方法,这个方法不仅仅是修改浏览器中地址栏的url,更重要的是会在window.history中替换当前页面的记录。如果不用location.replace方法,那么每次回退都会回退到上一个查询条件。当然,这样的需求可能对某些项目还有用。
最终解决方案
如果本文只是分享上面的解决思路,那价值就不大了。本文的价值应该是我们写的那个虽然简单但是却很强大的javascript类。
如果你看明白了上面的解决思路,那就看看这个简单的javascript类吧:
(function() {if (window.hashquery) {return;}window.hashquery = function() {};hashquery.prototype = {parsefromlocation: function() {if (location.hash === '' || location.hash.length === ) {return;}var properties = location.hash.substr().split('|');var index = ;for (var p in this) {if (!this.hasownproperty(p) || typeof this[p] != 'string') {continue;}if (index < properties.length) {this[p] = properties[index];if (this[p] === '-') {this[p] = '';}}index++;}},updatelocation: function() {var properties = [];for (var p in this) {if (!this.hasownproperty(p) || typeof this[p] != 'string') {continue;}var value = this[p];properties.push(value === '' ? '-' : value);}var url = location.origin + location.pathname + location.search + # + properties.join('|');location.replace(url);}};})();
这个类只有2个方法,hashquery.parsefromlocation() 方法从location.hash反序列化为hashquery子类的实例,hashquery.updatelocation() 方法将当前hashquery子类的实例序列化并更新到window.location。
可以看到hashquery这个类没有任何属性,那是因为我们只定义了一个基类,类的属性都在子类中进行定义。这也是符合实际的,因为查询条件都只有在具体的页面才知道有哪些属性。
另外,请注意这里的序列化和反序列化。这里的序列化仅仅是利用javascript反射机制将实例的所有字符串属性(按顺序)的值用“|”分隔;而序列化则是将字符串用“|”分隔后,再利用反射更新到实例的属性(按顺序)。
如何使用hashquery类
使用的时候就非常简单了。
第一步,定义一个子类,将需要用到的查询条件都加到字符串属性当中,如我们的代码:
(function() {window.tasksearchhashquery = function () {hashquery.constructor.call(this);this.iterationid = '';this.assigneduserid = '';this.status = '';this.keyword = '';};tasksearchhashquery.constructor = tasksearchhashquery;tasksearchhashquery.prototype = new hashquery();})();
第二步,在查询页面调用hashquery.parsefromlocation() 和 hashquery.updatelocation()方法即可。下面的代码是我们完整的查询页面:
(function() {var urls = {list: /app/task/list};var hashquery = null;var pager = null;$(document).ready(function () {hashquery = new tasksearchhashquery();hashquery.parsefromlocation();//在这里调用的哦,从location反序列化objectupdateformbyhashquery();$(#btnsearch).click(function() {updatehashquerybyform();hashquery.updatelocation();//在这里调用的哦,将查询条件序列化之后更新到location.hash$(#lblcount).html(加载中...);pager.reload();page.hidesearch();});pager = new listpager(#listtasks, urls.list);pager.getpostdata = function(index) {return pageindex= + index + &pagesize= + &projectid= + page.projectid+ &iterationid= + hashquery.iterationid+ &assigneduserid= + hashquery.assigneduserid+ &status= + hashquery.status+ &keyword= + hashquery.keyword;};pager.onloaded = function() {$(#lblcount).html(共 + $(#hfpagertotalcount).val() + 个任务);$(#hfpagertotalcount).remove();};pager.init();});function updatehashquerybyform() {hashquery.iterationid = $(#ddliterations).val();hashquery.assigneduserid = $(#ddlusers).val();hashquery.status = $(#ddlstatuses).val();hashquery.keyword = $(#txtkeyword).val();};function updateformbyhashquery() {$(#ddliterations).val(hashquery.iterationid);$(#ddlusers).val(hashquery.assigneduserid);$(#ddlstatuses).val(hashquery.status);$(#txtkeyword).val(hashquery.keyword);};})();
总结
这就是我们项目中使用location.hash来保存页面状态的全部知识了。不知道大家的web项目中是如何处理这样的需求的呢?
以上内容是小编给大家介绍的location.hash保存页面状态的技巧,希望对大家有所帮助!
其它类似信息

推荐信息