WebBrowser1.GoHome; //到浏览器默认主页
WebBrowser1.Refresh; //刷新
WebBrowser1.GoBack; //后退
WebBrowser1.GoForward; //前进
WebBrowser1.Navigate(‘...‘); //打开指定页面
WebBrowser1.Navigate(‘about:blank‘); //打开空页面
//打开空页面, 并写入...
WebBrowser1.Navigate(‘about:<head><title>标题</title><body>页面内容</body>‘);
//读取网页脚本中的变量:
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
i: Integer;
begin
s := WebBrowser1.OleObject.document.Script.str;
i := WebBrowser1.OleObject.document.Script.num;
ShowMessage(s); //Hello
ShowMessage(IntToStr(i)); //99
//也可以这样读:
s := WebBrowser1.OleObject.document.parentWindow.str;
i := WebBrowser1.OleObject.document.parentWindow.num;
ShowMessage(s); //Hello
ShowMessage(IntToStr(i)); //99
end;
//调用网页脚本中的函数:
procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.OleObject.document.parentWindow.MB(); //HTML-Js
//如需指定脚本语言, 需要:
WebBrowser1.OleObject.document.parentWindow.execScript(‘MB()‘,‘JavaScript‘); //HTML-Js
end;
//判断网页及内部框架网页是否全部下载完毕
procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
if not(<span style="font-family: Arial, Helvetica, sans-serif;">WebBrowser1</span>.Busy) then
ShowMessage(‘下载完毕‘);
end;
//改变背景色或背景图片:
WebBrowser1.OleObject.document.body.bgcolor := ‘#FF0000‘;
WebBrowser1.OleObject.document.body.background := ‘...图片地址‘;
//操作有 ID 标签的对象:
var
s: string;
begin
s := WebBrowser1.OleObject.document.getElementByIdx_x(‘span1‘).innerText;
ShowMessage(s); //这是 span1 标签中的内容
//或者:
s := WebBrowser1.OleObject.document.parentWindow.span1.innerText;
ShowMessage(s); //这是 span1 标签中的内容
//隐藏它:
WebBrowser1.OleObject.document.parentWindow.span1.style.display := ‘none‘;
end;
//获取网页源代码
var
s: string;
begin
s := WebBrowser1.OleObject.document.body.innerHTML; //body内的所有代码
s := WebBrowser1.OleObject.document.body.outerHTML; //body内的所有代码, 包含body标签
s := WebBrowser1.OleObject.document.documentElement.innerHTML; //html内的所有代码
end;