JS Capturing ActiveX Events

Source: Internet
Author: User

Recently participated in a project, my colleague in the development of an ActiveX object, I help him to write JS script called this object, which encountered a lot of problems, the most difficult is how to respond to the object returned by the ActiveX objects event. Right now, let's summarize it together.

First, let me introduce the COM component to illustrate how JS responds

COM components
Method:
void init ()//Initialize Object
Project CreateProject ()//Create an object of project type and return
Event:
OnInit (Int_code)//triggers when Init succeeds

Structure of the Project class:
Method:
void init ()//Initialize Project object
Event:
OnInit (Int_code)//triggers when Init succeeds


Then, we create an ActiveX object in the Web page.

<object id= "MyObj" classid= "clsid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ></object>


We then define global variables, obj and project, to hold instances of the objects and invoke their related methods

var obj = null; Global objects
var project = null; Project Object
Window.onload = function () {
obj = document.getElementById (' myobj '); Get Object
Obj.init (); Initializing objects
Project = Obj.createproject (); To create a Project object
Project.init (); Methods for invoking Project objects
};


The most common object event response method

The most common way to respond to object events is to use the tag syntax of the script event for, which is what Windows Media player does.

<script language= "Javascript" event= "OnInit (code)" for= "MyObj" >
if (code = = 0)
{
Todo:
}
</script>


The For property specifies that the object to which this event responds is myobj, note that the ID of the object tag is to be written here, not a JS variable
The event property specifies which events to respond to, and this is the OnInit event.


How to respond to object events returned by an ActiveX object

Now that the problem has come, we have called the CreateProject method and got a project object. This object also has events, so what should be the response?
Obviously, using the previous script event for method is not possible, because the For property cannot specify the value of the JS variable. Online search, found a very special method, is to use a double colon to define the function name, so the program is modified as follows:

var project = Obj.createproject ();
function Project::oninit (code)
{
if (code = = 0)
{
Todo:
}
}


However, after the operation has an error, saying that project is not defined, to think of it, JS will take precedence of the function definition, that is, define the function before executing the statement. Therefore, the function must be defined after project creation is successful, so the program is modified as follows:

var project = CreateProject ();
var fn = function () {//define a function that internally defines our callback function
callback function
function Project::oninit (code)
{
if (code = = 0)
{
Todo:
}
}
};
FN (); Execute this function, that is, define the callback function


Run a bit, and I can actually respond.



Postscript

I do not know you see the function project::oninit (code) such a way of writing, is it strange? I was surprised when I saw this, because I've never seen JavaScript with this syntax, which looks like defining a static function and defining a function in the project namespace as well. So I tested, found that Firefox does not recognize such a function definition, will report a grammatical error, indicating that this is not the standard JavaScript writing, is ie-only. Is it the JScript syntax? I looked through the JScript manual again, and I didn't see a similar syntax. On Microsoft's MSDN, there is no syntax reference for double colons found. However, I was in a forum and saw such a passage

A second script block, defines your event handler based on the global variable.  This have to is a separate script block, and the object have to is defined before this script block evaluates. Syntax is "function object::event (params)", where "object" is the name of the variable containing the object, "Event" is t He name of the even to which your ' re subscribing, and "params" is the set of parameters the event handler expects.


Maybe this is the way to get the ActiveX event handler.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.