简体   繁体   中英

Execute JavaScript in Delphi, get no results

I am trying to execute some javascript in Delphi 7. I fixed the original issue which was a missing semi-colon but no I just don't get any results. Seems like the oncalc is not being called, just a guess.

unit fMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  OleCtrls, SHDocVw, MSHTML;

type
  TfrmMain = class(TForm)
    WebBrowser1: TWebBrowser;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    HTMLWindow2: IHTMLWindow2;
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

uses
   ActiveX;


{$R *.dfm}

const

HTMLStrMap: AnsiString =
'<!DOCTYPE html> '+
'<html> '+
'  <head> '+
'    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> '+
'    <meta charset="utf-8"> '+
'    <title>Google Maps JavaScript API v3 Example: Directions Simple</title> '+
'    <link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet"> '+
'    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> '+
'    <script> '+
'      var directionsService = new google.maps.DirectionsService(); '+
'      var map; '+
''+
'      function initialize() { '+
'        directionsDisplay = new google.maps.DirectionsRenderer(); '+
'        var chicago = new google.maps.LatLng(41.850033, -87.6500523); '+
'        var mapOptions = { '+
'          zoom:7, '+
'          mapTypeId: google.maps.MapTypeId.ROADMAP, '+
'          center: chicago '+
'        }; '+
'        map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); '+
'        directionsDisplay.setMap(map); '+
'      } '+
''+
'      function calcRoute() { '+
'        var start = document.getElementById("start").value; '+
'        var end = document.getElementById("end").value; '+
'        var request = { '+
'            origin:start, '+
'            destination:end, '+
'            travelMode: google.maps.DirectionsTravelMode.DRIVING '+
'        }; '+
'        directionsService.route(request, function(response, status) { '+
'          if (status == google.maps.DirectionsStatus.OK) { '+
'            directionsDisplay.setDirections(response); '+
'          } '+
'        }); '+
'      } '+
'    </script> '+
'  </head> '+
'  <body onload="initialize()"> '+
'    <div> '+
'    <b>Start: </b> '+
'    <select id="start" onchange="calcRoute();"> '+
'      <option value="chicago, il">Chicago</option> '+
'      <option value="st louis, mo">St Louis</option> '+
'      <option value="joplin, mo">Joplin, MO</option> '+
'      <option value="oklahoma city, ok">Oklahoma City</option> '+
'      <option value="amarillo, tx">Amarillo</option> '+
'      <option value="gallup, nm">Gallup, NM</option> '+
'      <option value="flagstaff, az">Flagstaff, AZ</option> '+
'      <option value="winona, az">Winona</option> '+
'      <option value="kingman, az">Kingman</option> '+
'      <option value="barstow, ca">Barstow</option> '+
'      <option value="san bernardino, ca">San Bernardino</option> '+
'      <option value="los angeles, ca">Los Angeles</option> '+
'    </select> '+
'    <b>End: </b> '+
'    <select id="end" onchange="calcRoute();"> '+
'      <option value="chicago, il">Chicago</option> '+
'      <option value="st louis, mo">St Louis</option> '+
'      <option value="joplin, mo">Joplin, MO</option> '+
'      <option value="oklahoma city, ok">Oklahoma City</option> '+
'      <option value="amarillo, tx">Amarillo</option> '+
'      <option value="gallup, nm">Gallup, NM</option> '+
'      <option value="flagstaff, az">Flagstaff, AZ</option> '+
'      <option value="winona, az">Winona</option> '+
'      <option value="kingman, az">Kingman</option> '+
'      <option value="barstow, ca">Barstow</option> '+
'      <option value="san bernardino, ca">San Bernardino</option> '+
'      <option value="los angeles, ca">Los Angeles</option> '+
'    </select> '+
'    </div> '+
'    <div id="map-canvas" style="top:30px;"></div> '+
'  </body> '+
'</html> ';


procedure TfrmMain.FormCreate(Sender: TObject);
var
    aStream     : TMemoryStream;
begin
    WebBrowser1.Navigate('about:blank');
    if Assigned(WebBrowser1.Document) then
    begin
      aStream := TMemoryStream.Create;
      try
         aStream.WriteBuffer(Pointer(HTMLStrMap)^, Length(HTMLStrMap));
         aStream.Seek(0, soFromBeginning);
         (WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(aStream));
      finally
         aStream.Free;
      end;
      HTMLWindow2 := (WebBrowser1.Document as IHTMLDocument2).parentWindow;
    end;
end;

end.

The CSS for the example is a relative link to the example on the google site. If you are copying their example, either change it to be an absolute link or move the CSS file to your domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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