资讯专栏INFORMATION COLUMN

cookie,js检测是第一次加载还是刷新

Donald / 704人阅读

摘要:用于判断用户是否首次进入网站下面代码需要在服务器下调试参数错误读取错误用法第一次加载刷新

用于判断用户是否首次进入网站

下面代码需要在服务器下调试

        function Cookie(key, value) {
            this.key = key;
            if (value != null) {
                this.value = escape(value);
            }
            this.expiresTime = null;
            this.domain = null;
            this.path = "/";
            this.secure = null;
        }
        Cookie.prototype.setValue = function(value) {
            this.value = escape(value);
        }
        Cookie.prototype.getValue = function() {
            return (this.value);
        }
        Cookie.prototype.setExpiresTime = function(time) {
            this.expiresTime = time;
        }
        Cookie.prototype.getExpiresTime = function() {
            return this.expiresTime;
        }
        Cookie.prototype.setDomain = function(domain) {
            this.domain = domain;
        }
        Cookie.prototype.getDomain = function() {
            return this.domain;
        }
        Cookie.prototype.setPath = function(path) {
            this.path = path;
        }
        Cookie.prototype.getPath = function() {
            return this.path;
        }
        Cookie.prototype.Write = function(v) {
            if (v != null) {
                this.setValue(v);
            }
            var ck = this.key + "=" + this.value;
            if (this.expiresTime != null) {
                try {
                    ck += ";expires=" + this.expiresTime.toUTCString();;
                } catch(err) {
                    console.log("expiresTime参数错误");
                }
            }
            if (this.domain != null) {
                ck += ";domain=" + this.domain;
            }
            if (this.path != null) {
                ck += ";path=" + this.path;
            }
            if (this.secure != null) {
                ck += ";secure";
            }
            document.cookie = ck;
        }
        Cookie.prototype.Read = function() {
            try {
                var cks = document.cookie.split("; ");
                var i = 0;
                for (i = 0; i < cks.length; i++) {
                    var ck = cks[i];
                    var fields = ck.split("=");
                    if (fields[0] == this.key) {
                        this.value = fields[1];
                        return (this.value);
                    }
                }
                return null;
            } catch(err) {
                console.log("cookie读取错误");
                return null;
            }
        }

用法

            var ck=new Cookie("HasLoaded"); 
            if(ck.Read()==null){
                console.log("第一次加载");
                var dd = new Date();
                dd = new Date(dd.getYear() + 1900, dd.getMonth(), dd.getDate());
                dd.setDate(dd.getDate() + 365);
                ck.setExpiresTime(dd);
                ck.Write("true"); 
            }
            else{
                console.log("刷新");
            }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/82714.html

相关文章

  • 超详细的Java面试题总结(四 )之JavaWeb基础知识总结

    摘要:系列文章请查看超详细的面试题总结一之基础知识篇超详细的面试题总结二之基础知识篇超详细的面试题总结三之集合篇常见问题下面的都是自己之前在学习的时候总结的,对于巩固的基础知识应该有很大帮助。注意多线程的并发的读写类属性会导致数据不同步。 系列文章请查看: 超详细的Java面试题总结(一)之Java基础知识篇 超详细的Java面试题总结(二)之Java基础知识篇 超详细的Java面试题总结(...

    Leo_chen 评论0 收藏0
  • 请求网页时,怎么给我返回了一段 JavaScript 代码

    摘要:当断点被执行到时,你就可以查看右边的调用栈了,很容易就找到这个地方这个也是一个定时执行,不过他只执行一次,相当于定时炸弹。本文仅用于交流学习,请勿用于非法用途,后果自负首发地址请求网页时,怎么给我返回了一段代码 今天给大家带来的是一个论坛网站,牛仔俱乐部-努比亚社区, 网址为:https://bbs.nubia.cn/ showImg(https://segmentfault.com/...

    wthee 评论0 收藏0
  • WKWebView使用指南|功能丰富的JXBWKWebView

    摘要:使用这种方案拦截的网络请求造成的问题就是请求数据被清空,还是所为,看源码主要看代码中间那两句注释,大致的意思就是不会在进程间通信发送的。如何解决终极思路就是虽然的会在进程间通信时被丢弃,但不会。 github地址:JXBWKWebView,如果觉得项目不错可以点个star支持一下,谢谢~ 前言 目前iOS系统已经更新到iOS11,大多数项目向下兼容最多兼容到iOS8,因此,在项目中对...

    mist14 评论0 收藏0
  • WKWebView使用指南|功能丰富的JXBWKWebView

    摘要:使用这种方案拦截的网络请求造成的问题就是请求数据被清空,还是所为,看源码主要看代码中间那两句注释,大致的意思就是不会在进程间通信发送的。如何解决终极思路就是虽然的会在进程间通信时被丢弃,但不会。 github地址:JXBWKWebView,如果觉得项目不错可以点个star支持一下,谢谢~ 前言 目前iOS系统已经更新到iOS11,大多数项目向下兼容最多兼容到iOS8,因此,在项目中对...

    Cobub 评论0 收藏0

发表评论

0条评论

Donald

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<