首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

delphi 中执行 javascript的有关问题

2012-03-23 
delphi 中执行 javascript的问题!求高手指教function FindIEWebBrowser: IWebBrowser2vartmpShell: IShel

delphi 中执行 javascript的问题!求高手指教
function FindIEWebBrowser: IWebBrowser2;
var
  tmpShell: IShellWindows;
  tmpIntf: IDispatch;
  tmpIE: IWebBrowser2;
  i, Count: Integer;
begin
  try
  if FWebBrowser = nil then
  begin
  Count := 0;
  repeat
  tmpShell := CoShellWindows.Create;
  for i := 0 to tmpShell.Count - 1 do
  begin
  tmpIntf := tmpShell.Item(i);
  if tmpIntf = nil then
  continue;
  tmpIntf.QueryInterface(IID_IWebBrowser2, tmpIE);

  if tmpIE = nil then
  continue;
  if (Integer(Handle) = tmpIE.HWND) or
  FindIEWindow(Integer(tmpIE.HWND), Handle) then
  //这个话一直都报错 ,不知道为什么,
  //tmpIE 不为空啊 , IE浏览器已经打开了啊,不知道如何处理,求高手指点下  

  FWebBrowser := tmpIE;
  end;
  Inc(Count);
  Sleep(50);
  Application.ProcessMessages;
  until (FWebBrowser <> nil) or (Count > 50);
  end;
  Result := FWebBrowser;
  except
  end;
end;

function FindIEWindow(ParentHandle, ChildHandle: HWND): Boolean;
var
  tmpHandle: HWND;
begin
  tmpHandle := GetParent(ChildHandle);
  if tmpHandle = 0 then
  begin
  Result := false;
  Exit;
  end
  else
  begin
  if tmpHandle = ParentHandle then
  begin
  Result := true;
  Exit;
  end
  else
  begin
  Result := FindIEWindow(ParentHandle, tmpHandle);
  end;
  end;

end;

[解决办法]
eg:

//调用网页脚本中的函数:

procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.OleObject.document.parentWindow.MB(); //HTML-Js

//如需指定脚本语言, 需要:
WebBrowser1.OleObject.document.parentWindow.execScript('MB()','JavaScript'); //HTML-Js
end;
假如有这样的脚本:
<script>
function MB(){ 
alert('HTML-Js');
}
</script>

这样. 如果又返回值 就是你说的网址
只要你 ss:=WebBrowser1.OleObject.document.parentWindow.MB()
[解决办法]
你要执行的脚本在mainFrame里面,可以这样

WebBrowser1.Navigate('http://newmovie.gd.vnet.cn/gdvnet,0,tv_zhiqiang,0,11,1.page');

然后

webbrowser1.OleObject.document.parentWindow.execScript('chgPage(31)','JavaScript');

热点排行