0

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 ?

2
  • Login or not, idUser is never '0' ,so the execution will always go straight to the else block. Commented Feb 10, 2013 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 Commented Feb 10, 2013 at 16:49

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.