VUE前端cookie简单操作
程序员文章站
2022-06-24 17:21:56
如下是简单cookie操作,当前仅限前端实例,具体内容如下
要注意的有两点:
1、cookie内容存贮的名称
2、删除cookie是通过设置过期为过去时间实现...
如下是简单cookie操作,当前仅限前端实例,具体内容如下
要注意的有两点:
1、cookie内容存贮的名称
2、删除cookie是通过设置过期为过去时间实现的
<body> <div id="app"> <button @click="clearcookie()"> 清除cookie </button> </div> </body> <script> let app = new vue({ el: "#app", data: { }, created: function () { this.checkcookie(); }, methods: { //设置cookie setcookie: function (cname, cvalue, exdays) { var d = new date(); d.settime(d.gettime() + (exdays * 24 * 60 * 60 * 1000)); var expires = "expires=" + d.toutcstring(); console.info(cname + "=" + cvalue + "; " + expires); document.cookie = cname + "=" + cvalue + "; " + expires; console.info(document.cookie); }, //获取cookie getcookie: function (cname) { var name = cname + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charat(0) == ' ') c = c.substring(1); if (c.indexof(name) != -1) return c.substring(name.length, c.length); } return ""; }, //清除cookie clearcookie: function () { this.setcookie("username", "", -1); }, checkcookie: function () { var user = this.getcookie("username"); if (user != "") { alert("welcome again " + user); } else { user = prompt("please enter your name:", ""); if (user != "" && user != null) { this.setcookie("username", user, 365); } } } } }) </script>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。