Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2006-04-23
Medium Priority
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
  • 2
LVL 41

Expert Comment

ID: 16520046
 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

ID: 16520895

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

ID: 16520904

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

dbritt earned 2000 total points
ID: 16520919
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

575 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