How to execute javascript in delphi?

I am coding a small application, in the middle I hit at some point when I need to execute javascript to get my data?

in my process, I have to go into some kind of url and then go to some page and get data from this. I did all this with indy idhttp, I got all the information except one column that needs javascript to get the value, then I tried to use twebbowser to make it work for me, but how can I use cookies to enable in webbrowser ?

i the browsererto navigator (' http://mysite.com/login.php user and password'), and its login, and then I tried to access the following link like (' http://mysite.com/link1/ example.php '), but it redirects to the login page: (

any help appreciated :)

+7
javascript browser delphi
source share
2 answers

It would be best to automate IE itself. Take a copy of embeddedwb , drop the form, and navigate to the URL you need to execute. There is a component document property that returns OLEVariant, use it to execute a DHTML style statement .. something like document.form.submit; .

You can easily hide the window used for automation, one of the methods I used is to place it on a new page in the page control, add a second page to display the status, then display the status page and hide the tabs.

+2
source share

What is your question now? In the header, you are asking how to execute JavaScript. Try the following:

 uses MSHTML_TLB, SHDocVw, ShellAPI; function ExecuteScript(doc: IHTMLDocument2; script: string; language: string): Boolean; var win: IHTMLWindow2; Olelanguage: Olevariant; begin if doc <> nil then begin try win := doc.parentWindow; if win <> nil then begin try Olelanguage := language; win.ExecScript(script, Olelanguage); finally win := nil; end; end; finally doc := nil; end; end; end; 

Sample Usage:

 IDoc: IHTMLDocument2; Webbrowser1.Document.QueryInterface(IHTMLDocument2, iDoc); ExecuteScript(iDoc, 'document.login.submit()', 'JavaScript'); 

(Hereinafter can be found here ).


Then in the text you ask how to use cookies (when using TWebBrowser this should happen automatically). When using Indy HTTP, you just need to attach the TIdCookieManager to your TIdHTTPClient instance, that's all (but you probably don't want to use it anyway due to the script requirement ....)

+6
source share

All Articles