首页 > web前端 > js教程 > 关于JavaScript中cookie的用法的例子

关于JavaScript中cookie的用法的例子

高洛峰
发布: 2016-11-26 14:16:02
原创
1027 人浏览过

 最近做项目中牵扯到要写一些cookie的例子,发现在自己在js方面还有很多的不足,然需要努力:






无标题文档

<script><br> function cookie(name, value, options) {<br>    if (typeof value != 'undefined') { // name and value given, set cookie<br>        options = options || {};<br>        if (value === null) {<br>            value = '';<br>            options.expires = -1;<br>        }<br>        var expires = '';<br>        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {<br>            var date;<br>            if (typeof options.expires == 'number') {<br>                date = new Date();<br>                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));<br>            } else {<br>                date = options.expires;<br>            }<br>            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE<br>        }<br>        var path = options.path ? '; path=' + options.path : '';<br>        var domain = options.domain ? '; domain=' + options.domain : '';<br>        var secure = options.secure ? '; secure' : '';<br>        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');<br>    } else { // only name given, get cookie<br>        var cookieValue = null;<br>        if (document.cookie && document.cookie != '') {<br>            var cookies = document.cookie.split(';');<br>            for (var i = 0; i < cookies.length; i++) {<br>                var cookie = jQuery.trim(cookies[i]);<br>                // Does this cookie string begin with the name we want?<br>                if (cookie.substring(0, name.length + 1) == (name + '=')) {<br>                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));<br>                    break;<br>                }<br>            }<br>        }<br>        return cookieValue;<br>    }<br>};</p> <p>  $(document).ready(function(){<br>          if(cookie('show')==1)<br>            $(".introBuy").css("display", 'none');<br>       <br>       $("#close").click(function(){<br>           $(".introBuy").css("display", 'none');<br>           cookie('show',1)<br>            });<br>       <br>});<br></script>



关闭


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板