Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7708415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:36:24+00:00 2026-06-01T00:36:24+00:00

I am having this issue. I have a script that checks if variable exists,

  • 0

I am having this issue. I have a script that checks if variable exists, because some scripts load asyncronously, like FB for Facebook or twttr for Twitter.

function whenAvailable(name, callback, interval) {
    interval || (interval = 100); // ms
    window.setTimeout(function() {
        if ((window.hasOwnProperty && window.hasOwnProperty(name)) || window[name] || !!eval(name)) {
            return callback();
        } else {
            window.setTimeout(arguments.callee, interval);
        }
    }, interval);
}

Looks like this

if ((window.hasOwnProperty && window.hasOwnProperty(name)) || window[name] || !!eval(name)) 

does not work. IE throws error for
eval(name) — e.g. if name = ‘FB’, it says it can’t eval ‘FB’ which is undefined.

window.hasOwnProperty(name) does not work if name == 'twttr.widgets'.

Is there a universal and cross browser check for existence of var by var name?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:36:26+00:00Added an answer on June 1, 2026 at 12:36 am

    First off, you don’t need that eval (you almost never do), you can index into a JavaScript object using a string and bracketed notation, e.g.:

    window['FB'];
    

    or

    name = 'FB';
    foo = window[name];
    

    So to check:

    if (typeof window[name] === "undefined")
    

    …except that doesn’t differentiate between the property not existing at all, or existing but having the value undefined.

    or

    if (name in window)
    

    …but that checks the prototype as well as the object. It’s fine for window, though.

    window.hasOwnProperty(name) does not work if name == 'twttr.widgets'.

    Right, you have to break it up:

    var index = 0,
        parts = name.split('.'), // doesn't handle [] notation
        result;
    result = window;
    index = 0;
    try {
        while (typeof result !== "undefined" && result !== null && index < parts.length) {
            result = result[parts[index++]];
        }
    }
    catch (e) {
    }
    if (index < parts.length) {
        // Didn't find all of it
    }
    

    …or something to that effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having an issue with some Jquery script that I have and I
Thanks for helping with this. I have a script that checks logins and then
I have been having this issue in iterating through an array of keys and
I am having trouble with the following SQL statement. I have had this issue
I'm having an issue bending my head around this one. I have a table
I'm having an issue getting a LINQ query to work. I have this XML:
Howdy, I'm having a bit of an issue runnning a NAnt script that used
I have this script that basically will show a product or products based on
I'm having this issue that I've yet to find a solution to. For example,
I'm having a slight issue with the Prettify script that I'm trying to integrate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.