Source Code

Execute a JavaScript Code on a TWebBrowser Document

  • anonymous
  • Dec 19' 13
  • Delphi
  • 488
  • 50
  • United States

The TWebBrowser Delphi control provides access to the Web browser functionality from your Delphi applications.

Here's how to execute a custom script (JavaScript or VBScript) function on a HTML document loaded in the TWebBrowser control:


 uses MSHTML_TLB, SHDocVw;
 procedure ExecuteScript(doc: IHTMLDocument2; script: string; language: string) ;
    if doc  nil then
      if doc.parentWindow  nil then
        doc.parentWindow.ExecScript(script, Olevariant(language)) ;

Usage (in some Button OnClick event handler, for example): 

    script : string;
    //locate the first element with ID attribute = "main" and show its tag
    script := ''var elemMain = document.getElementById("main") ; if (elemMain != null) { alert(elemMain.tagName) ; }'';
    ExecuteScript(EmbeddedWB1.Document as IHTMLDocument2, script, ''javascript'')
  • Tags : delphi, javascript, browser
comments powered by Disqus

Related Code

Save a StringGrid as a CSV file

Empty Recylce Bin

Get MAC Address of Network Card

Kill Active Program

Resizable forms and size grip

Simple Like Function

CSS Pop UP with Javascript

Color Picker

Suspend the PC

Get current url