简体   繁体   English

无法从JavaScript调用Delphi ActiveX方法

[英]Can not call Delphi ActiveX method from JavaScript

I have issue with a Delphi ActiveX control. 我在使用Delphi ActiveX控件时遇到问题。 I create an ActiveX library and then an ActiveX form. 我先创建一个ActiveX库,然后创建一个ActiveX表单。 I define one function and I want to call this function from JavaScript. 我定义了一个函数,我想从JavaScript调用此函数。 But I can't. 但是我不能。 JavaScript throws an error: "Object doesn't support property or method 'Method1'". JavaScript引发错误:“对象不支持属性或方法'Method1'”。

This is the HTML code: 这是HTML代码:

<OBJECT id="GetDocsActiveX" classid="clsid:A03962E6-6030-46C0-988D-ADE26BC4BACD" codebase="GetDocs.ocx#version=1.0">
    <PARAM NAME="Color" VALUE="13417386">
</OBJECT>

This is the Delphi code *.ridl file 这是Delphi代码* .ridl文件

interface IGetDocs: IDispatch
{
    [id(0x000000E8)]
    HRESULT _stdcall Method1(void);
};

this is *_TLB.pas file 这是* _TLB.pas文件

IGetDocs = interface(IDispatch)
    ['{8F2BF1C6-98A5-4D6B-A43E-890698A3C91D}']   
    procedure Method1; safecall;
end;

and this is file with implementation 这是带有实现的文件

unit GetDocsU;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    ActiveX, AxCtrls, GetDocs_TLB, StdVcl, Vcl.StdCtrls, ShellApi, Vcl.XPMan, 
    IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;

type
    TGetDocs = class(TActiveForm, IGetDocs)    
protected    
     procedure Method1; safecall;
public
    { Public declarations }
     procedure Initialize; override;
end;

implementation

uses ComObj, ComServ;
{$R *.DFM}
procedure TGetDocs.Method1;

begin      
       MessageDlg('HI from active x', mtInformation, [mbOK], 0, mbOK);
end;

end.

Can anyone help with this issue. 任何人都可以帮助解决这个问题。 I try to move method in public section in published section without success. 我尝试在公开部分的公共部分移动方法,但没有成功。

EDIT here is java script 在这里编辑是Java脚本

<script type="text/javascript">
try {
        var obj = $("#GetDocsActiveX");
        if (obj) {
            obj.Method1();
        } else {
            alert("Object is not created!");
        }
    } catch (ex) {
        alert("Some error happens, error message is: " + ex.message);
    }
</script>

I noticed at least this one error: 我至少注意到了这一错误:

var obj = $("#GetDocsActiveX");

That looks like a line of jQuery (or similar library) to get the element with the id 'GetDocsActiveX'. 看起来像一行jQuery(或类似的库)来获取ID为'GetDocsActiveX'的元素。 But jQuery doesn't return the element directly. 但是jQuery不会直接返回该元素。 It returns a jQuery object that wraps a collection of elements. 它返回一个包装了元素集合的jQuery对象。 You try to call the method of that jQuery object instead of the actual element you're looking for. 您尝试调用该jQuery对象的方法,而不是要查找的实际元素。

There are ways to unravel that element from the jQuery collection, but I think it's easier to just get the object using plain JavaScript: 有多种方法可以从jQuery集合中解散该元素,但我认为使用纯JavaScript获得对象会更容易:

var obj = document.getElementById("GetDocsActiveX");

or if you enjoy working with selectors: 或者,如果您喜欢使用选择器:

var obj = document.querySelector("#GetDocsActiveX");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM