How to avoid javascript errors when activeX object doens't exist?


I am trying to build a web page that use the values returned by function from my activeX control. However, that control might not always be present, therefor ajavscript return an erro because I am refering to unknown methods.

Is there a way to aoid those errors without having to use try{}ctach(){} every time I am refering to my object?

<HTML><HEAD>
<SCRIPT language="javascript">
<!--
function onLoaded(){
    if(ActiveX)
      var v=ActiveX.test();
}
//-->
</SCRIPT>
      </HEAD>
<BODY onload="onLoaded();">
<OBJECT id="ActiveX" height="0" width="0" classid="clsid:DD0E9F89-55AC-4D52-9BA6-209536B3A36A"></OBJECT>
</BODY>
</HTML>
gammaeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HonorGodSoftware EngineerCommented:
 Well, an alternative would be to have one wrapper function that is used to access this activeX control, and have it contain the try/catch.
0
dbrittCommented:
gammae,

Are you saying that the object unloads itself after it has been loaded and stops working after it had already been working? Or just that it is never loaded? If it is neverloaded (of course the element will still exist), you could check for a single method and if that method exists, the others should also, like this:

if(obj && obj.test)
{
   alert("All's well!");
}

Otherwise, if the object is actually unloading itself, the solution to your problem will depend on one thing.

*** Do you plan on continuing execution of the function after the point where the ActiveX object "disappears"? ***

If yes, you must use try/catch or use HonorGod's suggestion about wrapper functions.

If no, I recommend catching the error in the window.onerror event and handling it there. See example below:

=====================================================================

<html>
      <head>
            <script language="JavaScript">
            <!--
                  window.onerror = function(msg, url, line)
                  {
                        if(msg == "Object doesn't support this property or method")
                        {
                              // We assume this error came from the ActiveX object
                              // because you wouldn't leave syntax errors in your
                              // production code, now would you?
                  
                              alert("Ah ha! Our ActiveX object has gone AWOL!");
                              
                              Debug.writeln(msg + " at " + url + " (line: " + line + ")");
                              
                              return true; // Cancel the nasty client error
                        }
                  }

                  // Don't put events in HTML code :)
                  window.onload = function()
                  {
                        if(myAX)
                        {
                              var v = myAX.test();
                        }
                  }
            //-->
            </script>
      </head>

      <body>
            <object id="myAX" classid="clsid:DD0E9F89-55AC-4D52-9BA6-209536B3A36A" style="height:0; width:0;"></object>
      </body>
</html>

=====================================================================

I hope this helps you, enjoy!
0
gammaeAuthor Commented:


My question is only about not triggering javascript error for a client that do not have the activeX component installed on his machine.
0
dbrittCommented:
Then you may use the window.onerror and handle it however you wish (alert the user, ignore it, whatever).

You can branch in your code before you try using the object and simply not let them execute code that requires it.

You can do try/catch everywhere the object is accessed.

Or you can use HonorGod's suggestion about wrapper functions that call the object's methods.

Take your pick :)



Here's an example of a wrapper function:

function AXTest()
{
     if(AX)
     {
          try
          {
               AX.test();
          }
          catch(e)
          {
               // Do whatever
          }
     }
}

...

<object id="AX" etc etc etc></object>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.