当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - how to tell dynamically if object exists, from a string variable

问题描述:

I Would like to tell if an object has been successfully created when loaded in via ajax call to an external javascript at run time. This is required because the loaded script can sometimes process data for 20 seconds prior to creating a global scoped variable (that is can have various names), and i need to wait until that script has finished prior to using the object.

i would like a function i could call that would behave like this

function is_object_loaded( objectname )

{

if (typeof objectname !== "undefined" ) {

// do something

}

}

is_object_loaded("demographic_pail");

obviously i need to turn objectname into something other than a string; and i refuse to eval unless its totally unavoidable.

Thanks!

网友答案:
function is_object_loaded( objectname ) {
  if (typeof window[objectname] !== undefined) {
      // do  something
  }
}
网友答案:

You could user async:false property of ajax and everything waits until that ajax is executed

 $.ajax({
  ....
  async:false,
  });
分享给朋友:
您可能感兴趣的文章:
随机阅读: