Hi !

I have the following problem: Having a web page loaded into a TWebBrowser
hosted in my Delphi application, I want to have Delphi code execute a
JavaScript function embedded in the HTML page. How can I do that ?

To help understand what I mean, I've done a sample program (see below)
loading an HTML page into a TWebBrowser and having a JavaScript function
named "MyFunction". I want to have Delphi code (let's say a button's event
handler) calling that JavaScript. How can I do that ?

procedure TForm1.Button1Click(Sender: TObject);
var
    sl       : TStringList;
    ms       : TMemoryStream;
    HTMLCode : String;
const
    CRLF = #13#10;
begin
    HtmlCode := '<HTML>' +
                '<HEADER>' +
                '<SCRIPT type="text/javascript">' + CRLF +
                '  function MyFunction() {' + CRLF +
                '    alert("Hello !");' + CRLF +
                '  }' + CRLF +
                '</SCRIPT>' + CRLF +
                '</HEADER>' +
                '<BODY>' +
                'Hello Delphi !' +
                '</BODY>' +
                '</HTML>';

    WebBrowser1.Navigate('about:blank');
    while WebBrowser1.ReadyState < READYSTATE_INTERACTIVE do
        Application.ProcessMessages;

    if Assigned(WebBrowser1.Document) then begin
        sl := TStringList.Create;
        try
            ms := TMemoryStream.Create;
            try
                sl.Text := HTMLCode;
                sl.SaveToStream(ms);
                ms.Seek(0, 0);
                (WebBrowser1.Document as
IPersistStreamInit).Load(TStreamAdapter.Create(ms));
            finally
                ms.Free;
            end;
        finally
            sl.Free;
        end;
    end;
end;

Regards,
--
francois.pie...@overbyte.be
Author of ICS (Internet Component Suite, freeware)
Author of MidWare (Multi-tier framework, freeware)
http://www.overbyte.be

_______________________________________________
Delphi mailing list -> Delphi@elists.org
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi

Reply via email to