views:

381

answers:

2

Hi, I have a Delphi application which loads a Google map in a TWebBrowser:

with WebBrowser1.Document as IHTMLDocument2 do
  with parentWindow do
    ExecScript('map.setCenter(new GLatLng(51.15917, 4.13889),10)', 'JavaScript');

Any idea of how to get the extents of the map in my application?
TIA
Steven

edit
Rob's answer points to a partial solution: javascript knows about the coordinates. I would like to get them into my Delphi application.

note on Expert's Exchange
(long answer to davidins' reply)
I was on Expert's Exchange when they started and when it was still free. I left there when they got greedy. If somebody is nice enough to help me out on a technical problem, I don't mind paying the guy a drink for it, but I definitely don't want to pay somebody else for it.
And their 30-day trial is even worse. Why do I have to submit my credit card number if they're not going to charge it?
"Experts Exchange is the most trusted IT Resource on the internet and we are confident that you will agree" (sic). Well, I wouldn't be too sure, EE. I like SO a lot more, appreciate any help I can get and try to give answers whenever I can (which unfortunately is not often).

+3  A: 
davidivins
Sorry, no access to Expert's Exchange. Can't you post the solution here? (see long answer appended to OP)
stevenvh
David has already included the solution: Set the value of a hidden HTML form field, and then read that element's value from the host application.
Rob Kennedy
A: 

IHTMLWindow2.execScript from the mentioned EE example should return the return value of the executed script as a Variant. But you don't have to use IHTMLDocument2.parentWindow property. There's also IHTMLDocument.Script which is an IDispatch so you can use it via Variant late binding:

var
  Document: IHTMLDocument;
  VScript, V: Variant;
begin
  Document := WebBrowser.Document as IHTMLDocument;
  VScript := Document.Script;
  V := VScript.HelloJavaScript();
  ShowMessage(V);
end;

HelloJavaScript is a javascript function returning a string:

<script language="javascript">
function HelloJavaScript()
{
    s = "Hello, world! (javascript)";
        alert(s);
        return s;
}
</script>
TOndrej
Maybe ExecScript *should* return the result as a Variant, but it doesn't. The function is documented to always set the result parameter to VT_EMPTY.
Rob Kennedy
That's interesting, thanks. Fortunately, the IHTMLDocument.Script method works.
TOndrej