3

I want to give condition method in my JavaScript code. This code does not work in Internet Explorer 10 version. When I paste this code in JavaScript validator, this message is shown:

Function declarations should not be placed in blocks. Use a function expression or move the statement to the top of the outer function.

How can I have a function here?

            if(jQuery("header").attr("id") == "header-style-two")
        {
                    function sticky_relocate() {
                        var window_top = jQuery(window).scrollTop();
                        var div_top = jQuery('#sticky-anchor').offset().top;
                        if (window_top > div_top) {
                            jQuery('.mainmenu-area').removeClass('light-menu');
                            //This is for when div in top         
                        } else {
                            jQuery('.mainmenu-area').addClass('light-menu');
                            //This is for when div in not top
                        }
                    }

                    jQuery(function () {
                        jQuery(window).scroll(sticky_relocate);
                        sticky_relocate();
                    }); 
        }   
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171

3 Answers3

6

You can't have named function in an if block (it is not valid by the spec and hoisting would make it rather confusing anyway, but some JavaScript engines may try to compensate for bad code, which is why it may work in some browsers), but you can assign functions to variables in an if block.

Rather than:

function sticky_relocate() {

You can do this:

var sticky_relocate = function() {

So something like this will do the trick:

if(jQuery("header").attr("id") == "header-style-two")
{
    var sticky_relocate = function() {
        var window_top = jQuery(window).scrollTop();
        var div_top = jQuery('#sticky-anchor').offset().top;
        if (window_top > div_top) {
            jQuery('.mainmenu-area').removeClass('light-menu');
            //This is for when div in top         
        } else {
            jQuery('.mainmenu-area').addClass('light-menu');
            //This is for when div in not top
        }
    }

    jQuery(function () {
        jQuery(window).scroll(sticky_relocate);
        sticky_relocate();
    }); 
}
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
1

JSHint is giving you a warning, which means, "we think this is not the best idea, but we'll allow you to do it."

You can change your code in a couple of ways.

Change the function declaration to a function expression:

if(jQuery("header").attr("id") == "header-style-two") {
  var sticky_relocate = function() {
    var window_top = jQuery(window).scrollTop();
    var div_top = jQuery('#sticky-anchor').offset().top;
    if (window_top > div_top) {
      jQuery('.mainmenu-area').removeClass('light-menu');
      //This is for when div in top         
    } else {
      jQuery('.mainmenu-area').addClass('light-menu');
      //This is for when div in not top
    }
  }
  jQuery(function () {
    jQuery(window).scroll(sticky_relocate);
    sticky_relocate();
  }); 
}

OR move the function definition out of the if block.

if(jQuery("header").attr("id") == "header-style-two") {
  jQuery(function () {
    jQuery(window).scroll(sticky_relocate);
    sticky_relocate();
  }); 
}

function sticky_relocate() {
  var window_top = jQuery(window).scrollTop();
  var div_top = jQuery('#sticky-anchor').offset().top;
  if (window_top > div_top) {
    jQuery('.mainmenu-area').removeClass('light-menu');
    //This is for when div in top         
  } else {
    jQuery('.mainmenu-area').addClass('light-menu');
    //This is for when div in not top
  }
}
therobinkim
  • 2,500
  • 14
  • 20
0

You can't have function declaration under if condition. You can put it outside and call it based on the condition.

var isHeaderStyleTwo = (jQuery("header").attr("id") == "header-style-two");
function sticky_relocate() {
  var window_top = jQuery(window).scrollTop();
  var div_top = jQuery('#sticky-anchor').offset().top;
  if (window_top > div_top) {
    jQuery('.mainmenu-area').removeClass('light-menu');
    //This is for when div in top         
  } else {
    jQuery('.mainmenu-area').addClass('light-menu');
    //This is for when div in not top
  }
}

jQuery(function () {
  if (isHeaderStyleTwo) {
      jQuery(window).scroll(sticky_relocate);
        sticky_relocate();
  }
Mouli S
  • 1
  • 1