用户选择cookie集/检查jQuery [英] User select cookie set/check with jQuery

查看:110
本文介绍了用户选择cookie集/检查jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个带有语言选择的模态窗口,如果用户选择option1模态隐藏,并且存储了一个cookie,因此用户不会再次看到模态。如果选择option2,则页面重定向并存储cookie,因此页面将根据cookie每次重定向用户。

It's a modal window with language select, if user chose option1 the modal hides, and a cookie is stored, so the user won't see the modal again. If option2 is chosen, the page redirects and a cookie is stored so the page will redirect the user according to the cookie every time.

当前代码重定向用户,即使option1 cookie设置,我不知道如何单独检查cookie。

The current code redirects the user even if option1 cookie is set, I don't know how to check the cookies separately.

编辑:借助于@Miloš和@balexandre的工作代码:

The working code with the help of both @Miloš and @balexandre:

$(document).ready(function(){
var myurl = "http://domain.com/";
//var currenturl = $(location).attr('href');
//console.log(myurl, location.href);
if (myurl == location.href) {

    var lang = $.cookie('lang');
    if (lang) {
        if (lang == 'es') {
            window.location.href = "http://domain.com?lang=es";
        }
    }
    else {
var _message_to_show = 'Chosse your preferred language<br/><a href="#" id="modal_close">ENGLISH</a><span id="lang_right"><a href="http://domain.com?lang=es" id="modal_exit">ESPANOL</a></span>';

    $.fancybox(
        _message_to_show,
        {
        'width'             : 350,
        'height'            : 300,
        'transitionIn'      : 'none',
        'transitionOut'     : 'none',
        'centerOnScroll'    : 'true',
        'overlayOpacity'    : 0.9,
        'overlayColor'      : '#000',
        'modal'             : 'true'
        }
    );

    $('#modal_close').live('click', function(e) {
     $.cookie("lang", "en", { path: '/', expires: 7 });
        e.preventDefault();
        $.fancybox.close();
    });

    $('#modal_exit').live('click', function(e) {
    $.cookie("lang", "es", { path: '/', expires: 7 });
        e.preventDefault();
        $.fancybox.close();
        window.location.href = "http://domain.com?lang=es";
    });
}
} else {
}
});


推荐答案

我会说你做错了开始。你试图存储用户选择语言的信息,它只能是一种语言,对吗?为什么要存储两个不同的cookie,内容为true?例如,您可以存储一个名为lang的cookie,并将其内容设置为en或es或用户选择的任何语言。然后你会有:

I'd say you are doing it wrong from the beginning. You are trying to store the information about user's choice of language, and it can be only one language, right? Why store two different cookies with contents "true"? You can store a single cookie, called "lang", for example, and set it's contents to "en" or "es" or whatever language the user chooses. Then you would have:

var lang = $.cookie('lang');
if (lang) {
    if (lang == 'es') {
        redirect
    }
}
else {
    display modal
}

如果你绝对需要用不同的cookie,扩展现有的CMS或框架或任何内容,你应该打破你的分离,如此:

If you absolutely have to do it with different cookies, for example because you are extending an, existing CMS or framework or whatever, you should break your if apart like so:

if (!$.cookie('en')) {
    if (!$.cookie('es')) {
        display modal
    }
    else {
        redirect
    }
}

这篇关于用户选择cookie集/检查jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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