简体   繁体   English

如何在JavaScript中检查空Cookie?

[英]How to check for a null cookie in javascript?

The following switch statement is not catching a cookie that does not exist. 以下switch语句未捕获不存在的cookie。

switch (stateCookie) {
                    case 'Virginia':
                        window.location = '/Teacher-Login/VA';
                        break;
                    case 'North Carolina':
                        window.location = '/Teacher-Login/NC';
                        break;
                    case 'South Carolina':
                        window.location = '/Teacher-Login/SC';
                        break;
                    case null:
                        window.location = '/Teacher-Login/VA';
                        break;
                    case '':
                        window.location = '/Teacher-Login/VA';
                    default:
                        window.location = '/pages/state-login/login.html';

Ok, I have tried several things but I am still inexplicably having trouble with this. 好的,我已经尝试了几件事,但是我仍然莫名其妙地遇到了麻烦。 Here is the code that I have at this moment: 这是我目前的代码:

$('#welcome select').change(function () {
    var stateSelected = $('option:selected', this).val();
    var stateCookie = getCookie("ia_state");
    if(stateSelected != stateCookie)
    {
        setCookie('ia_state', stateSelected);
        switch (stateCookie) {
            case 'Virginia':
            case null:
            case '':
                window.location = '/Teacher-Login/VA';
                break;
            case 'North Carolina':
                window.location = '/Teacher-Login/NC';
                break;
            case 'South Carolina':
                window.location = '/Teacher-Login/SC';
                break;
            default:
                window.location = '/pages/state-login/login.html';
}
}
}); 

The switch statement is being evaluated but it is always going to the default case. switch语句正在评估中,但始终使用默认情况。 I have yet to figure out how to catch it if the cookie is null/undefined. 我还没有弄清楚如果cookie为null / undefined时如何捕获它。

You are missing a break: 您错过了休息时间:

                ...
                case '':
                    window.location = '/Teacher-Login/VA';
                    break;
                default:
                    window.location = '/pages/state-login/login.html';
                    break;

mdn has item function: mdn具有项目功能:

function hasItem(sKey) { 
    return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); 
}  

I agree with Mike Stewart. 我同意麦克·斯图尔特的观点。 You are missing a break, so if it's returning as '', then it will go on to the default statement. 您错过了一个休息时间,因此如果返回的是“”,它将继续执行默认语句。 If you are still getting odd results after changing that, then it's possible the problem is somewhere else. 如果更改该结果后仍然得到奇怪的结果,则可能是问题出在其他地方。

var loc = '';
switch (stateCookie) {
    case 'Virginia':
    case null:
    case '':
    case undefined:
        loc = '/Teacher-Login/VA';
        break;
    case 'North Carolina':
        loc = '/Teacher-Login/NC';
        break;
    case 'South Carolina':
        loc = '/Teacher-Login/SC';
        break;
    default:
        loc = '/pages/state-login/login.html';
        break;
};
window.location = loc;

EDIT: added undefined case 编辑:添加了未定义的大小写

Something like: 就像是:

if( $.cookie('cookiename') == null ) {
   /// is null
}

Validate your cookie before it enters in the switch, like: 在Cookie进入交换机之前对其进行验证,例如:

stateCookie = (typeof stateCookie != 'string')?'':stateCookie;

Then the switch. 然后开关。

So, remove the case null from your switch. 因此,从您的交换机中删除case null。

case '':
    window.location = '/Teacher-Login/VA';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM