通过JavaScript写cookies [英] Writing cookies via JavaScript

查看:129
本文介绍了通过JavaScript写cookies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码打开一个html弹出窗口,其中希望添加一个不再显示按钮,将存储在cookie中的首选项,以便弹出不会再显示
为什么不工作?



这是应该写入cookie的行:

  document.cookie =dontShow = 1; path = /; expires = Wednesday,01-Jan-2020 12:00:00 GMT; domain = .qpcftw.co.cc; 

更新:问题是Cookie未储存, alert(document.cookie);



以下是完整的JS代码:

  / *************************** / 
// @作者:Adrian yEnSMato Gondelle
// @网站:www.yensdesign.com
// @电子邮件:yensamg@gmail.com
// @许可:随意使用它,但保持这个学分请!
/ *************************** /
var popupStatus = 0;

$(document).ready(function(){
if(document.cookie!=dontShow = 1; path = /; expires = Wednesday,01-Jan-2040 12: 00:00 GMT; domain = .qpcftw.co.cc){
$(#backgroundPopup)。css({
opacity:0.75
});
$(#backgroundPopup)。fadeIn(1000);
$(#popupContact)。fadeIn(1000);
popupStatus = 1;
}
// CLOSING POPUP
//不再显示
$(#dontShow)。click(function(){
document.cookie =dontShow = 1; path = expires = Wednesday,01-Jan-2020 12:00:00 GMT; domain = .qpcftw.co.cc;
alert(document.cookie); // 4debugging
disablePopup();
});
//点击x事件
$(#popupContactClose)click(function(){
disablePopup();
});
//点击事件!
$(#backgroundPopup)click(function(){
disablePopup();
});
//按Esc键事件!
$(document).keypress(function(e){
if(e.keyCode == 27&& popupStatus == 1){
disablePopup();
}
});
});

//禁用jQuery魔术的弹出窗口!
function disablePopup(){
//仅当启用时禁用弹出窗口
if(popupStatus == 1){
$(#backgroundPopup)。fadeOut ;
$(#popupContact)。fadeOut(500);
popupStatus = 0;
}
}


解决方案

this:



小提琴演示

  document.cookie ='dontShow = 1; expires = Wed,01 Jan 2020 12:00:00 GMT; path = /'; 

function readCookie(option){
var optionEQ = option +'=';
var ca = document.cookie.split(';');

for(var i = 0; i var c = ca [i];

while(c.charAt(0)===''){
c = c.substring(1,c.length);
}

if(c.indexOf(optionEQ)=== 0){
return c.substring(optionEQ.length,c.length);
}
}

return false;
}

if(!readCookie('dontShow')){
//运行弹出代码
}



修改:或者,作为自定义jQuery插件:



插件版本Fiddle

  document.cookie ='dontShow = 1; expires = Wed,01 Jan 2020 12:00:00 GMT; path = /'; 

(函数($){
$ .fn.readCookie = function(option){
var optionEQ = option +'=';
var ca = document .cookie.split(';');

for(var i = 0; i var c = ca [i];

while(c.charAt(0)===''){
c = c.substring(1,c.length);
}

if (c.indexOf(optionEQ)=== 0){
return c.substring(optionEQ.length,c.length);
}
}

return false;
}
})(jQuery);

if(!$(document).readCookie('dontShow')){
$('#cookie-status')。html('< p> Cookie未设置。 ; / p>');
} else {
$('#cookie-status')。html('< p> Cookie已设置。< / p&
}

希望这有助于! :)


I am using this code to open an html pop up, to which is wish to add a "don't show again" button that will store a preference in a cookie so that the pop up won't be shown again Why isn't it working?

this is the line that should write the cookie:

document.cookie = "dontShow=1; path=/; expires=Wednesday, 01-Jan-2020 12:00:00 GMT; domain=.qpcftw.co.cc";

Update: the problem is that the cookie isn't stored, and the "alert(document.cookie);" (see below) shows that the cookie is null (nothing is shown).

Here's the full JS code:

/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
/***************************/
var popupStatus = 0;

$(document).ready(function(){
        if (document.cookie != "dontShow=1; path=/; expires=Wednesday, 01-Jan-2040 12:00:00 GMT; domain=.qpcftw.co.cc"){
        $("#backgroundPopup").css({
            "opacity": "0.75"
        });
        $("#backgroundPopup").fadeIn(1000);
        $("#popupContact").fadeIn(1000);
        popupStatus = 1;
        }
    //CLOSING POPUP
        //Don't show again
        $("#dontShow").click(function(){
            document.cookie = "dontShow=1; path=/; expires=Wednesday, 01-Jan-2020 12:00:00 GMT; domain=.qpcftw.co.cc";
            alert(document.cookie); //4debugging
            disablePopup();
        });
        //Click the x event!
        $("#popupContactClose").click(function(){
            disablePopup();
        });
        //Click out event!
        $("#backgroundPopup").click(function(){
            disablePopup();
        });
        //Press Escape event!
        $(document).keypress(function(e){
            if(e.keyCode==27 && popupStatus==1){
                disablePopup();
            }
        });
});

//disabling popup with jQuery magic!
function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut(500);
        $("#popupContact").fadeOut(500);
        popupStatus = 0;
    }
}

解决方案

Try this:

Fiddle Demo

document.cookie = 'dontShow=1; expires=Wed, 01 Jan 2020 12:00:00 GMT; path=/';

function readCookie(option) {
    var optionEQ = option + '=';
    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, c.length);
        }

        if (c.indexOf(optionEQ) === 0) {
            return c.substring(optionEQ.length, c.length);
        }
    }

    return false;
}

if (!readCookie('dontShow')) {
    //run your popup code
}

Edit: Or, as a custom jQuery plugin:

Plugin version Fiddle

document.cookie = 'dontShow=1; expires=Wed, 01 Jan 2020 12:00:00 GMT; path=/';

(function($) {
    $.fn.readCookie = function(option) {
        var optionEQ = option + '=';
        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, c.length);
            }

            if (c.indexOf(optionEQ) === 0) {
                return c.substring(optionEQ.length, c.length);
            }
        }

        return false;
    }
})(jQuery);

if (!$(document).readCookie('dontShow')) {
    $('#cookie-status').html('<p>Cookie not set.</p>');
} else {
    $('#cookie-status').html('<p>Cookie has been set.</p>');
}

Hope this helps! :)

这篇关于通过JavaScript写cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆