Click here to Skip to main content
15,909,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a javaScript object "top.ui.cmn.iFrame".
I use if(top.ui.cmn.iFrame) to check if iFrame property is null.
However, if "top" is null, the above will give a script error. Is there any elegant way of checking if the objects within the object are not null rather than doing
if(top && top.ui && top.ui.cmn && top.ui.cmn.iFrame){
Posted
Updated 12-Jul-11 7:54am
v3

1 solution

You could use try ... catch as well as/instead of your if statement:

try {
  if(top.ui.cmn.iFrame) {
    // top.ui.cmn.iFrame true
  } else {
    // top.ui.cmn.iFrame false and top.ui.cmn exists
  }
} catch(e) {
  // top or top.ui or top.ui.cmn undefined
}
 
Share this answer
 
Comments
hasithabst 12-Jul-11 14:38pm    
Thanks Graham. I was wondering if any JS function exists to check it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900