Operate COM controls using JavaScript

Source: Internet
Author: User

Objective: To use JavaScript scriptsCodeCall the COM control to implement basic functions:
1. Define and initialize the COM control;
2. Call the COM interface;
3. Register com events and call events.

1. Define and initialize the COM control
Define an object tag on the HTML page to load the COM control:
1) classid: the number of the COM control, used to uniquely identify a COM control;
2) ID: it is the object accessed by javascript when calling the COM control. It is a global object. Here, the global object is named OCX;
3) codebase: The resource path of the cab package for com. You can add # version =, to release the cab for automatic update and download. Jquery together, 17 jquery

Jquery together, 17 jquery

 
<Object classid = "CLSID: D4CCE975-3B00-417A-A520-B265FBFEB178" id = "OCX" codebase = "carplayctrl. Cab" standby = "Control Loading..."> </Object> content from 17 jquery

[You can also define other related attributes]

Content from 17 jquery

 

Open the page with a browser (ie kernel) and then you can load the COM control.

2. Call COM by port and attribute
2.1 call Method
A Global Object OCX is defined when the com tag is created. Therefore, it is very easy to call the control interface in JavaScript code to display it later, similar to the call stated in later Java, such as calling the method for creating a player:

COM interface: content from 17 jquery

 
Long createplayer (long flag, ulong dectag, BSTR streamername, long playerindex)

Content from 17 jquery

Jquery together, 17 jquery

 

Javascript calls the COM interface:

Jquery together, 17 jquery

 

 
VaR RTN = OCX. createplayer (0, "dectag", "guanhuistreamer",-1); If (RTN <0) {// todo}

Jquery together, 17 jquery

Jquery together, 17 jquery

Jquery together, 17 jquery

 

Createplayer needs to pass four parameters in JavaScript (only integer and string types are involved here), and return a value of the numerical type as a judgment flag.

17jquery.com

 

2.2 call attributes
The call attribute is the same as the method, but you do not need to pass the parameter to com. For example, you can obtain the attribute of the player image quantity: The content is from 17 jquery.

 
VaR screennums = OCX. screennum; 17jquery.com

Jquery together, 17 jquery

Of course, you can also set the attribute value, for example, set the number of player images:

Content from 17 jquery

 

 
OCX. screennum = 4;

Content from 17 jquery

Jquery together, 17 jquery

3. Register com events and implement function callback
We can call the com methods and attributes to access and set the com.
But this is not enough, because we cannot always take the initiative to call com methods or attributes to obtain data. For example, to obtain the current state of COM (the State is constantly changing), we need to use round robin (such as window. setinterval () constantly accesses the interface, which is time-consuming and a waste of system resources.
Therefore, if COM has a notification event, we can register the JavaScript function to the com event. When com executes the event and calls the event, we can indirectly call the JavaScript function for callback. In this way, JavaScript can easily passively receive data.

The following are a series of implementation processes that notify JavaScript to respond to changes in the player window focus:
A) activescreenchanged (long playerindex): It is a notification event in COM. No parameter is required during registration.
B) regocxactivescreen (): A Registration event in JavaScript;
C) listener_activescreen (playerindex): It is a callback function in Javascript. No parameter is required during registration.

1) define JavaScript registration event: content from 17 jquery

Function regocxactivescreen () {var focus = document. getelementbyid ("OCX"); if ($. browser. MSIE) {// The Registration Method of IE kernel browser focus. attachevent ("activescreenchanged", listener_activescreen);} else {// non-ie kernel browser registration method // Of course there is no need here, because only the IE kernel browser can execute the COM component focus discussed here. addeventlistener ("activescreenchanged", listener_activescreen );}}

Content from 17 jquery

Content from 17 jquery

[You can also directly use OCX. attachevent ("activescreenchanged", listener_activescreen); to register it, Because ocx has been defined before, and an alias is used here.]

2) define the Javascript callback function:

Content from 17 jquery

 

 
Function listener_activescreen (playerindex) {comment ('{currentwin'{.html ('playing window: '+ playerindex); // todo}

Content from 17 jquery

17jquery.com

17jquery.com

 

Another method of calling is directly implemented through the script tag on the page, for example:

Content from 17 jquery

 

 
<Script language = "JavaScript" for = "OCX" event = "ocxmethod (parm)"> alert (parm); // todo </SCRIPT>

Content from 17 jquery

I personally do not like this method, and the code structure integrity is poor.

Related Article

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.