Reputation: 761
A) I have a first function which works very well :
if(onlyUs == '1' && idUser == '0'){
obj.after(jQuery('<div />').css({'clear':'both'}).addClass('kklike-msg').text('Only registered users can vote.'));
setTimeout(function(){
jQuery('.kklike-msg').fadeOut('normal');
},3000);
return false;
}
B) So I thought I could do the following thing :
if(idUser == '0'){
if(action == 'like'){
var ajaxAction = 'add_like';
}else{
var ajaxAction = 'remove_like';
}
}else{
if(action == 'like'){
var ajaxAction = 'add_like';
window.open('http://mywebsite.com/like')
}else{
var ajaxAction = 'remove_like';
window.open('http://mywebsite.com/remove')
}
}
C) Knowing that the original function is simply (works well):
if(action == 'like'){
var ajaxAction = 'add_like';
}else{
var ajaxAction = 'remove_like';
}
But B) is not working. In both condition (Login or not), the new window is going to open. Do you have a solution ?
Upvotes: 0
Views: 18912
Reputation: 103377
Without knowing the type of idUser
, it is difficult to tell what the problem is, but the most likely culprit is the use of ==
for comparison instead of ===
. JavaScript will convert the variables being compared into similar types when using ==
, which can cause some very unpredictable results in your case.
I recommend writing your code like the following. If this still does not work as you expected, you should investigate what the value of idUser
actually is. It may not be a string
which would be the cause of your problem.
if (idUser === '0') {
if(action === 'like') {
var ajaxAction = 'add_like';
} else {
var ajaxAction = 'remove_like';
}
} else {
if (action === 'like') {
var ajaxAction = 'add_like';
window.open('http://mywebsite.com/like');
} else {
var ajaxAction = 'remove_like';
window.open('http://mywebsite.com/remove');
}
}
For a very simple example of why you should use ===
, see this blog post:
http://longgoldenears.blogspot.com/2007/09/triple-equals-in-javascript.html
Upvotes: 6