使用JScript的HTA中的ActiveX事件处理程序 [英] ActiveX event handlers in an HTA using JScript

查看:112
本文介绍了使用JScript的HTA中的ActiveX事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我可以编写如下的事件处理程序:

In C# I can write event handlers as follows:

var wdApp = new Microsoft.Office.Interop.Word.Application();
wdApp.DocumentBeforeSave += (Document doc, ref bool saveAsUI, ref bool cancel) => {
   //do stuff here
};

在VBA/VB6中,我可以使用静态事件处理:

In VBA/VB6, I can use static event handling:

Dim WithEvents wdApp As Word.Application

Private Sub wdApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
    'do stuff here
End Sub

我更喜欢使用动态事件处理.但是,即使在JScript中,即使在此处中使用静态事件处理时,也是如此:

I would prefer to use dynamic event handling. However, in JScript, even when using static event handling with the syntax described here:

var wdApp = new ActiveXObject('Word.Application');
wdApp.Visible = true;

function wdApp::Quit() {
    window.alert('Quit');
};

失败:

0x800a138f-JScript运行时错误:预期对象

0x800a138f - JScript runtime error: Object expected

此外,由于可以将声明标记为Private,因此在VBA/VB6中可以选择使用静态事件处理.但是,在JScript中,变量和处理程序都必须在全局范围内声明.

Also, static event handling is an option in VBA/VB6, because the declarations can be marked Private. However, in JScript, both the variable and the handler have to be declared in the global scope.

两个问题:

  1. 如何在HTA环境中使用JScript处理自动化创建的对象的事件? (注意:我知道在WSH中可以使用传递给CreateObject的前缀和名为wdApp_Quit的函数,但是我正在寻找HTA解决方案.)

  1. How can I handle events of Automation-created objects with JScript in an HTA environment? (Note: I know that it is possible in WSH using a prefix passed to CreateObject, and a function named wdApp_Quit, but I am looking for an HTA solution.)

如何在不污染全局范围的情况下做到这一点?


此处有一个较旧的问题.. >


There is an older question here.

推荐答案

错误似乎是由于

  1. 在Javascript/JScript中,首先声明函数声明,在初始化变量之前.好像代码是这样写的:

  1. In Javascript/JScript function declarations are evaluated first, before the variable is initialized. It's as if the code was written thus:

 var wdApp;
 function wdApp::Quit() { ... }
 wdApp = new ActiveXObject('Word.Application');

  • Microsoft JScript解析器将特殊命名的声明(带有::)解释为将函数附加为事件处理程序的指令.

  • The Microsoft JScript parser interprets the specially-named declaration (with ::) as an instruction to attach the function as an event handler.

    但是在声明时,处理程序无法附加到wdApp引用的对象,因为此时的wdApp仍然是undefined.因此,错误.

    But at the point of the declaration, the handler cannot be attached to the object referred to by wdApp, because wdApp at that point is still undefined. Hence, the error.

    解决方案是强制在初始化wdApp之后评估函数声明.这可以通过以下三种方式之一完成: 1 :

    The solution is to force the function declaration to be evaluated after wdApp is initialized. This can be done in one of three ways1:

    1. 由于函数声明仅悬挂在函数范围内,因此将函数声明包装在IIFE中:

    1. Since the function declaration is hoisted only to within the function scope, wrap the function declaration in an IIFE:

     var wdApp = new ActiveXObject('Word.Application');
     (function() {
         function wdApp::Quit() {
             //do stuff here
         }
     })();
    

  • 使用某种字符串创建声明->代码机制-evalsetTimeout和字符串,window.execScriptnew Function:

  • Create the declaration using some sort of string -> code mechanism -- eval, setTimeout with a string, window.execScript, or new Function:

     var wdApp = new ActiveXObject('Word.Application');
     eval('function wdApp::Quit() { ... }`);
    

  • 在当前SCRIPT块之前初始化变量. 脚本事件文章中的大多数示例都是通过设置在SCRIPT块之前的某些元素上的id属性:

  • Initialize the variable before the current SCRIPT block. Most of the examples in the Scripting Events article do this by setting the id property on some element before the SCRIPT block:

    <object progid="ordersystem.clsorder" id="myorder" events="true"/>
    <script language="jscript">
        function myorder::onNew() {
          WScript.Echo("new order received from myorder")
        }
    //...
    

    ,但是也可以使用多个SCRIPT块来完成:

    but this could also be done using multiple SCRIPT blocks:

        <SCRIPT>
            var wdApp = new ActiveXObject('Word.Application');
        </SCRIPT>
        <SCRIPT>
            function wdApp::Quit() {
                //do stuff here
            }
        </SCRIPT>
    


    就污染全局名称空间而言,只有第三个变体要求wdApp位于全局名称空间中;其他两个变体可以包装在IIFE中.


    As far as polluting the global namespace, only the third variant requires wdApp to be in the global namespace; the other two variants can be wrapped in an IIFE.

    1.从技术上讲,在IE和HTA下有第四种方法,但是它涉及到非标准HTML而不是非标准Javascript.但是,仅当使用HTML OBJECT标记而不是new ActiveXObject( ... )声明对象时,才可以使用它.

    1. Technically, there is a fourth way under IE and HTAs, but it involves non-standard HTML instead of non-standard Javascript; But it can only be used if the object is declared using the HTML OBJECT tag, not with new ActiveXObject( ... ).

    <script for="wdApp" event="Quit">
        //do stuff here
    </script>
    

    这篇关于使用JScript的HTA中的ActiveX事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆