Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript/jQuery, if and else statements both being executed

I'm not too sure what's going on here and feel I may be missing something that is probably quite obvious, but I have an if else in which both statements are being called. If someone could shed some light on this that would be great. The code below is using asual and should be detecting whether or not a div $('#' + $.address.hash() has already been loaded. However both if and else events are being fired in order. In fact the else statement fires twice...

$('#lmit_back').show(400, function() {
    if($('#' + $.address.hash() ).length == 0) {
        $('#init').load('inithandler.php', { 'address' : $.address.hash() }, function() {
            $('#' + $.address.hash()).fadeIn(1000, function() {
                $('#remn').load('pagehandler.php',{ 'address' : $.address.hash() });
            });
        });
    }  
    else {
        alert('hey');
    }
});

Here is all the code..I can't really work out what could cause it to execute twice, unless it has something to do with address.change which im not as familiar with as I would like to be.

if (($.address.hash() == '')) {$.address.hash('home');}

    $('#lmit_back').click(function() {

            $.address.hash('home');

    });


    $.address.change( function() {  



        if (!($.address.hash() == 'home')) 
        {



        var exception = '.' + $('[href="#' + $.address.hash() + '"]').parent().attr('class');


            $('#left_menu_bar li:not(' + exception + ')').hide(300,function() {

                $(exception).show( function() {

                    $('#left_menu_bar').animate({ width:'100%'},400, function() { 

                        $('#lmit_back').show(400, function() {


                                if ($('#' + $.address.hash() ).length === 0)
                                {

                                    $('#init').load('inithandler.php', { 'address' : $.address.hash() } , function() { 

                                        $('#' + $.address.hash()).fadeIn(1000, function() {

                                            $('#remn').load('pagehandler.php',{ 'address' : $.address.hash() });

                                        });



                                    });

                                }
                                else
                                {

                                alert('nigs');

                                }

                        });

                    });

                });

            });



        }
        else
        {




            $('#left_menu_bar').animate({ width:'251px'}, function() { 

                $('#left_menu_bar li').show( function() {

                    $('#lmit_back').hide();

                });

            });



            $('#left_menu_bar').css('width','251px');



        }


    }); 
like image 984
Melbourne2991 Avatar asked Sep 18 '12 02:09

Melbourne2991


2 Answers

The problem here is not arising from the code you have pasted. The only way the code could be running multiple times and hitting multiple branches is if it is being executed more than once. Look to the surrounding code for places where this could be called more than once.

like image 156
weexpectedTHIS Avatar answered Oct 27 '22 18:10

weexpectedTHIS


I faced similar issue, while debugging when if condition was true it also went in else block. Later I added alert and console.log in if/else block's & realized, else was not actually being executed but while debugging it looked like it was in else.

So, whoever faces same issue verify by adding alert or console.log.

like image 35
priyanka_rao Avatar answered Oct 27 '22 18:10

priyanka_rao