标签:
项目需要,怎样让用户点击浏览器后退按钮刷新后退页面的验证码,通过cookie来解决
本想通过控制html的http-equiv属性来解决问题,如下
<meta http-equiv="Expires" content="0"> <meta http-equiv="Cache-Control" content="no-cache"> <meta http-equiv="Pragma" content="no-cache">
本想通过控制response的setHeader方法来解决问题,如下
@RequestMapping(value = "login", method = RequestMethod.GET) public String login(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException { Object params = request.getSession().getAttribute("params"); if (params != null) model.addAttribute("message", params); response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", -10); return "login"; }
结论:点击浏览器的后退按钮,压根就没再从后台返回新的数据,依旧从浏览器的缓冲中读取
目标页面 js login.js
var Cookie = { // 设置Cookie setCookie: function(name, value, expires, path, domain){ document.cookie = name + "=" + escape( value ) + ( ( expires ) ? ";expires=" + expires.toGMTString() : "" ) + ( ( path ) ? ";path=" + path : "" ) + ( ( domain ) ? ";domain=" + domain : "" ); }, // 获取Cookie getCookie: function( name ){ var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); if(arr != null) return unescape(arr[2]); return null; }, // 删除Cookie delCookie: function( name ){ var d = new Date(); d.setTime(d.getTime() - 3600 * 1000); this.setCookie(name, "", d); } } console.log(Cookie.getCookie("a")); if (Cookie.getCookie("a") != 1){ Cookie.setCookie("a",1); console.log(Cookie.getCookie("a")); window.location.reload(); }
目标页面下一页 js
var Cookie = { // 设置Cookie setCookie: function(name, value, expires, path, domain){ document.cookie = name + "=" + escape( value ) + ( ( expires ) ? ";expires=" + expires.toGMTString() : "" ) + ( ( path ) ? ";path=" + path : "" ) + ( ( domain ) ? ";domain=" + domain : "" ); }, // 获取Cookie getCookie: function( name ){ var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); if(arr != null) return unescape(arr[2]); return null; }, // 删除Cookie delCookie: function( name ){ var d = new Date(); d.setTime(d.getTime() - 3600 * 1000); this.setCookie(name, "", d); } } Cookie.setCookie("a",2);
总结:通过cookie跨页面保存变量的功能,判断同一变量在不同页面设置的值来达到区分的目的
标签:
原文地址:http://www.cnblogs.com/guDouMaoNing/p/4324464.html