Compile COM component instances in Javascript
This section describes how to write COM components in JavaScript. Based on the steps described in the previous section, readers can easily find that Using VBScript to write COM components is exactly the same.
First, determine the requirement: We will use JavaScript to implement a set of case-insensitive letter conversion functions and publish them as the COM component method. Although it is a very "pediatric" example, it is a complete COM component implementation journey!
We name the COM component carrier. WSC file lower2upper. WSC, as shown in the code list 23-1.
Code List 23-1 use JavaScript to compile the COM component instance -- lower2upper. WSC
1. <? XML version = "1.0" encoding = "UTF-8"?>
2. <? Component error = "true" DEBUG = "true"?>
3. <component id = "tuchb. Javascript. com. lowertoupper">
4.
5. <Registration
6. Description = "string uppercase/lowercase letter conversion"
7. progid = "tuchb. Javascript. com. lowertoupper"
8. Version = "1.0"
9. classid = "{9b88510f-9d5b-4dcd-9068-8ab0b4f7999c}" remotable = "true">
10. </registration>
11.
12. <public>
13.
14. <! -- Define Method -->
15. <method name = "tolower">
16. <parameter name = "str"/>
17. </method>
18. <method name = "toupper">
19. <parameter name = "str"/>
20. </method>
21.
22. </Public>
23.
24. <script language = "JavaScript">
25. <! [CDATA [
26. Function tolower (STR)
27 .{
28. var result = Str. tolowercase ();
29. Return result;
30 .}
31. Function toupper (STR)
32 .{
33. var result = Str. touppercase ();
34. Return result;
35 .}
36.]>
37. </SCRIPT>
38.
39. </component>
Use the following command line to register the lower2upper. WSC file:
Regsvr32 lower2upper. WSC
To ensure successful registration, readers can find the program id "tuchb. Javascript. com. lowertoupper" in the registry. The result should not be surprising.
Next, you can use any language tool that can call the COM component to find and call the COM component you just compiled. Here, ASP is used as an example, as shown in the code list 23-2.
Code List 23-2 call the com routine -- lower2upper. asp
1. <HTML>
2. 3. <title> call the COM component to convert uppercase and lowercase letters </title>
4. 5. <body>
6. <%
7. Str = "I love China ."
8. Set OBJ = server. Createobject ("tuchb. Javascript. com. lowertoupper ")
9. response. Write (STR & "->" & obj. tolower (STR) & "<p> ")
10. response. Write (STR & "->" & obj. toupper (STR) & "<p> ")
11. Set OBJ = nothing
12. %>
13. </body>
14. Start IIS, deploy lower2upper. asp in a directory with Script permissions, and access it in the browser.