Reputation: 705
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.
The current code redirects the user even if option1 cookie is set, I don't know how to check the cookies separately.
EDIT: 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 {
}
});
Upvotes: 0
Views: 614
Reputation: 2279
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
}
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
}
}
Upvotes: 1