1

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 ?

Mathieu
  • 797
  • 2
  • 6
  • 24
  • Login or not, `idUser` is never `'0'` ,so the execution will always go straight to the `else` block. – Teemu Feb 10 '13 at 16:10
  • 1
    this code does nothing really: if(action == 'like') { var ajaxAction = 'add_like'; } else { var ajaxAction = 'remove_like'; } as soon as you are out of the if, the declared variables go away – Mark Schultheiss Feb 10 '13 at 16:49

1 Answers1

6

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

Community
  • 1
  • 1
Dan Herbert
  • 99,428
  • 48
  • 189
  • 219
  • @Teemu, Which is why it really is important to know what the value of `idUser` is in this case. There could be code executing before this block setting the value to `1`, `'1'`, `true`, etc. Without the value of `idUser`, the reason this code is failing is unclear. – Dan Herbert Feb 10 '13 at 16:43