Maison  >  Article  >  interface Web  >  关于JavaScript中cookie的用法的例子

关于JavaScript中cookie的用法的例子

高洛峰
高洛峰original
2016-11-26 14:16:021023parcourir

 最近做项目中牵扯到要写一些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>



关闭


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn