XWidgetSoft Forum https://bbs.xwidget.com/ |
|
How to check if object exists https://bbs.xwidget.com/viewtopic.php?f=8&t=3009 |
Page 1 of 1 |
Author: | meme [ April 30th, 2013, 7:57 pm ] |
Post subject: | How to check if object exists |
Using Javascript, I have a loop that modifies the Attributes of some objects, how is it possible to check if the object exists before applying changes ? I have tried several methods to test if the object exist, including - if (typeof objectname == "undefined") if (null == objectname) if ("undefined" == typeof(objectname) But any reference to an object that does not exist result in a "runtime error object does not exist" ![]() Can anyone assist ? ![]() |
Author: | qiancang [ April 30th, 2013, 8:47 pm ] |
Post subject: | Re: How to check if object exists |
you can input all objects you want to change into array. |
Author: | digigamer [ April 30th, 2013, 9:04 pm ] |
Post subject: | Re: How to check if object exists |
meme wrote: Using Javascript, I have a loop that modifies the Attributes of some objects, how is it possible to check if the object exists before applying changes ? I have tried several methods to test if the object exist, including - if (typeof objectname == "undefined") if (null == objectname) if ("undefined" == typeof(objectname) But any reference to an object that does not exist result in a "runtime error object does not exist" ![]() Can anyone assist ? ![]() Code: if(typeof(mybogusobject) != 'undefined') { //exists print("exists"); } else { //not exists print("not exists"); } Your only error was using double quotes. Use single quotes. And typeof is a method it should be declared as typeof(object) |
Author: | meme [ April 30th, 2013, 9:56 pm ] |
Post subject: | Re: How to check if object exists |
Hi qiancang, I am not sure what you mean by Quote: you can input all objects you want to change into array. I still want to check they exist before proceeding. Hi digigamer Modified code as you suggested, now the line if(typeof(mybogusobject) != 'undefined') returns "runtime error mybogusobject is undefined" when mybogusobject does not exist. if mybogusobject does exist it works OK. Any ideas ? Thank you both very much for your replies ![]() |
Author: | digigamer [ May 1st, 2013, 3:44 am ] |
Post subject: | Re: How to check if object exists |
That should not happen... As of my understanding on JScript. Well here's another way. You can enclose in a try-catch block too. But before that please run this test widget. Attachment: It includes all the procedures used in JS that to detect the validity of an object. Please go through the code-behind. Run the test. If it is successful it will write success. If any error is thrown, it will show error. In this case I'll assume that your script engine and/or some other code has a problem, coz that's the only way to test it. Try running my code and say. SUGGESTION: A good program (here widget) never manipulates on non existent, uninitialized or nulled objects. I wonder what's your requirement. ![]() ![]() |
Author: | meme [ May 1st, 2013, 12:47 pm ] |
Post subject: | Re: How to check if object exists |
Thank you digigamer I have already enclosed it in a try catch block to skip the error but as you suggested this is not a good solution. Your test widget works OK returning "Obj Doesnot exist" ![]() I have modified the script to only address objects that already exist. Thanks again for you help, |
Page 1 of 1 | All times are UTC - 8 hours |
Powered by phpBB® Forum Software © phpBB Group http://www.phpbb.com/ |