0

How can you set a var to something you want to later check if it's defined or not?

Example: To check if jQuery is not defined, you'd do this:

if (typeof(jQuery) === 'undefined') { 
}

But what if I want to do something like this (this obviously doesn't work):

   var toCheckLater = jQuery;   // This fails.

   // Some time later.. 
   if (typeof(toCheckLater) === 'undefined') {
   }

What I'm trying to do is dynamically load scripts from an array, but I want to set ahead of time the variable whose definition I'll check for later. And I'd like to avoid a big block of ifs or switch statement. Meaning I'm hoping to find a solution a bit more elegant than:

switch (scriptName) {
  case 'jQuery': 
    if (typeof(jQuery) === 'undefined') {
    }
    break;
  case 'someOtherScriptName':
  .
  .
  .
}

Any ideas? Thanks in advance.

Kon
  • 27,113
  • 11
  • 60
  • 86

7 Answers7

2

A function would do:

var toCheckLater = function() { return typeof jQuery == "undefined"; }

// later:
toCheckLater()

You might also use a fabric for such functions:

function getChecker(name) {
    return function() {
        return typeof window[name] == "undefined";
        // alternative:
        return name in window; // can be undefined, but the variable exists
    };
}
var toCheckLater = getChecker("jQuery");
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Damn, so simple... I can't believe I didn't think of setting it as a function. I'm very disappointed with myself (and others who answered without reading the question), but very happy with your help. Thanks a lot! – Kon Aug 03 '12 at 14:14
  • Just to follow up... the function approach worked better for me, because I'm also checking typeof($.receiveMessage). – Kon Aug 03 '12 at 15:13
1

Use

if (typeof jQuery === "undefined")

to check for undefined.

Waleed Khan
  • 11,426
  • 6
  • 39
  • 70
  • That doesn't help. I did however forget the typeof part, so I updated my question. – Kon Aug 03 '12 at 14:03
1

I don't quite get what you're trying to achieve, but I think you could do something like this

var toCheckLater = typeof jQuery; //if, for example, jQuery is defined you'll get "function"

// Some time later.. 
if (toCheckLater === 'undefined') {

}
nicosantangelo
  • 13,216
  • 3
  • 33
  • 47
  • I want to check if it's defined later. Your suggestion would check if defined right away and then give me an old value later. – Kon Aug 03 '12 at 14:06
  • Aww ok, I understood (a little late :P). I hope I was able to help a bit anyways – nicosantangelo Aug 03 '12 at 14:24
1

Simply do this:

var scriptName = 'jQuery';

if( !window.hasOwnProperty(scriptName) ){
    //jQuery is undefined
} 
Engineer
  • 47,849
  • 12
  • 88
  • 91
0
var checker = function(scriptArray) {
  for(var i in scriptArray) 
    this[i] = i?i:loadScript(i); // make your own loadScript function
}

checker({'jquery','anothercode'});

//access checker.jquery / checker.anothercode

You could use something like this. Create a 'class' that load all the scripts.

0

You have use the typeof Method

typeof(variable_name) will give you "string", "object", "undefined" or "number" depending on the variable

typeof(jQuery) == "undefined"

is the way to go.

Rishav Rastogi
  • 15,484
  • 3
  • 42
  • 47
0

Your array of scripts:

var scripts = ['script1', 'jQuery'];

After loading the scripts you can use the same array and loop through it.

for (var i = 0, n = scripts.length; i !== n; i++) {
    if (!window.scripts[i]) {
        // falsy
    }
}

This assumes that the scripts array references a global variable that the script will expose when loaded.

However, if I understand what you're going for, a better method would be to register a callback for when the appropriate script element is loaded. See these questions for some examples: (1), (2)

Code like this is pretty standard:

function loadScriptAsync(src, callback) {
    var script = document.createElement('script'),
        place = document.getElementsByTagName('script')[0];

    script.type = "text/javascript";
    script.async = true;
    script.src = src;
    script.onload = script.onreadystatechange = function() {
        callback();
        // clean up for IE and Opera
        script.onload = null;
        script.onreadystatechange = null;
    };

    place.parentNode.insertBefore(script, place);
}
Community
  • 1
  • 1
Radu
  • 8,561
  • 8
  • 55
  • 91