6

in another question on SO, I was determining how to toggle off a function, and the working solution is this:

I place var disabledFlag = true; in the head section of my page and before calling shell.js, then in shell.js I have:

/*******************************/
/*  TOGGLE BUTTON
/*******************************/
var toggleBlock = function() {
    console.log(disabledFlag);
    if(!disabledFlag){
      var windowsize = $(window).width(),
      isDesktop = windowsize > 765;
      $("#quicksearch").toggleClass("collapse in", isDesktop);
      $("#quicksearch").toggleClass("collapse out", !isDesktop);
      $("#sidebar").toggleClass("collapse in", isDesktop);
      $("#sidebar").toggleClass("collapse out", !isDesktop);
    }
    else {
      $("#quicksearch").addClass("collapse out");
      $("#sidebar").addClass("collapse out");  
    }
}
$(document).ready(toggleBlock);
$(window).on("resize.showContent", toggleBlock);
toggleBlock();

shell.js is an common file that is shared with other sites and may not have the variable defined. how do I check if the variable is defined, and if not assign it to false and then execute the code above?

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
TEN Design
  • 717
  • 3
  • 13
  • 31

4 Answers4

6

try

if (typeof disabledFlag === 'undefined')
    disabledFlag = false;
Dwza
  • 6,494
  • 6
  • 41
  • 73
3

There are easier ways to do it than using ternaries or if else statements.

As far as your specific function goes, you could do something like this:

var toggleBlock = function() {
    var disabledFlag = disabledFlag||false;

    if(!disabledFlag){
    //etc.

undefined is falsy, so it works with the logical || operator. That || operator makes the new var disabledFlag be set to disabledFlag (if it exists), and if not, it will set the var to false

This same concept is used in many different contexts. For example:

Situation 1 -

var obj = {hello: 'world'};

function fn(object) {
    var object = object || {foo: 'bar'};

    return object;
}

fn(obj)  //  ==> {hello: 'world'}

Situation 2 -

function fn(object) {
    var object = object || {foo: 'bar'};

    return object;
}

fn(objectThatDoesntExist); //  ==> {foo: 'bar'}

In JavaScript libraries and module-pattern projects, this concept is used quite frequently in many different ways.

Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • mehhh still not working, what your saying makes sense so i have no idea why this isnt working – TEN Design Feb 16 '14 at 01:30
  • this is magically working now. i am assuming that server level cache expired. we automatically generate htmlc files in each instance and once it expried, this started working. – TEN Design Feb 18 '14 at 03:39
1

You don't need typeof

if (window.disabledFlag === undefined) window.disabledFlag = false;
citizenslave
  • 1,408
  • 13
  • 25
0

you can check if a variable is undefined with the typeof keyword, like this:

if(typeof neverDeclared == "undefined") //no errors

if(neverDeclared == null) //throws ReferenceError: neverDeclared is not defined

take a look here for more info on typeof

Jonathan Schneider
  • 26,852
  • 13
  • 75
  • 99