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

Posted on 2006-04-23
Last Modified: 2008-03-03

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?

<SCRIPT language="javascript">
function onLoaded(){
      var v=ActiveX.test();
<BODY onload="onLoaded();">
<OBJECT id="ActiveX" height="0" width="0" classid="clsid:DD0E9F89-55AC-4D52-9BA6-209536B3A36A"></OBJECT>
Question by:gammae
    LVL 41

    Expert Comment

     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.
    LVL 15

    Expert Comment


    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:


                <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()
                                  var v = myAX.test();

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


    I hope this helps you, enjoy!

    Author Comment


    My question is only about not triggering javascript error for a client that do not have the activeX component installed on his machine.
    LVL 15

    Accepted Solution

    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()
                   // Do whatever


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

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now