在Delphi中执行JavaScript,没有结果

时间:2013-03-18 14:36:31

标签: javascript delphi delphi-7

我正在尝试在Delphi 7中执行一些javascript。我修复了原始问题,这是一个缺少的分号,但不,我只是没有得到任何结果。似乎oncalc没有被调用,只是猜测。

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.

1 个答案:

答案 0 :(得分:2)

该示例的CSS是指向Google网站上示例的相对链接。如果要复制他们的示例,请将其更改为绝对链接或将CSS文件移动到您的域。