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.

7 Answers 7

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");
Sign up to request clarification or add additional context in comments.

2 Comments

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!
Just to follow up... the function approach worked better for me, because I'm also checking typeof($.receiveMessage).
1

Use

if (typeof jQuery === "undefined")

to check for undefined.

1 Comment

That doesn't help. I did however forget the typeof part, so I updated my question.
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') {

}

2 Comments

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.
Aww ok, I understood (a little late :P). I hope I was able to help a bit anyways
1

Simply do this:

var scriptName = 'jQuery';

if( !window.hasOwnProperty(scriptName) ){
    //jQuery is undefined
} 

Comments

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.

Comments

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.

Comments

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);
}

Comments

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.