WebBrowser1.GoHome; //到瀏覽器默認(rèn)主頁 WebBrowser1.Refresh; //刷新 WebBrowser1.GoBack; //后退 WebBrowser1.GoForward; //前進(jìn) WebBrowser1.Navigate(''...''); //打開指定頁面 WebBrowser1.Navigate(''about:blank''); //打開空頁面 //打開空頁面, 并寫入... WebBrowser1.Navigate(''about:<head><title>標(biāo)題></title><body>頁面內(nèi)容 </body>''); //讀取網(wǎng)頁腳本中的變量: 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; 假如網(wǎng)頁中有這樣的語句:<br> <script><br> var<br> str = "Hello";<br> i = 99;<br> </script> //調(diào)用網(wǎng)頁腳本中的函數(shù): procedure TForm1.Button1Click(Sender: TObject); begin WebBrowser1.OleObject.document.parentWindow.MB(); //HTML-Js //如需指定腳本語言, 需要: WebBrowser1.OleObject.document.parentWindow.execScript(''MB ()'',''JavaScript''); //HTML-Js end; 假如有這樣的腳本:<br> <script><br> function MB(){ <br> alert (''HTML-Js'');<br> }<br> </script> //判斷網(wǎng)頁及內(nèi)部框架網(wǎng)頁是否全部下載完畢 procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant); begin if WebBrowser1.Application = pDisp then begin Text := ''網(wǎng)頁下載完畢!''; end; end; //改變背景色或背景圖片: WebBrowser1.OleObject.document.body.bgcolor := ''#FF0000''; WebBrowser1.OleObject.document.body.background := ''...圖片地址''; //操作有 ID 標(biāo)簽的對象: var s: string; begin s := WebBrowser1.OleObject.document.getElementByID (''span1'').innerText; ShowMessage(s); //這是 span1 標(biāo)簽中的內(nèi)容 //或者: s := WebBrowser1.OleObject.document.parentWindow.span1.innerText; ShowMessage(s); //這是 span1 標(biāo)簽中的內(nèi)容 //隱藏它: WebBrowser1.OleObject.document.parentWindow.span1.style.display := ''none''; end; 假如網(wǎng)頁中有這樣的內(nèi)容:<br> <span id=span1>這是 span1 標(biāo)簽中的內(nèi) 容</span><br> //獲取網(wǎng)頁源代碼 var s: string; begin s := WebBrowser1.OleObject.document.body.innerHTML; //body內(nèi)的所有代 碼 s := WebBrowser1.OleObject.document.body.outerHTML; //body內(nèi)的所有代 碼, 包含body標(biāo)簽 s := WebBrowser1.OleObject.document.documentElement.innerHTML; //html 內(nèi)的所有代碼 end; //獲取網(wǎng)頁全部源代碼 uses ActiveX; var ms: TMemoryStream; begin if not Assigned(WebBrowser1.Document) then Exit; ms := TMemoryStream.Create; (WebBrowser1.Document as IPersistStreamInit).Save (TStreamAdapter.Create(ms), True); ms.Position := 0; Memo1.Lines.LoadFromStream(ms, TEncoding.UTF8); // Memo1.Lines.LoadFromStream(ms, TEncoding.Default); {GB2312 等雙字節(jié) } ms.Free; end; //WebBrowser 中的右鍵菜單 //先要添加ApplicationEvents1,指定其Message事件 //屏蔽右鍵菜單 procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); begin with Msg do begin if not IsChild(WebBrowser1.Handle, hWnd) then Exit; Handled := (message = WM_RBUTTONDOWN) or (message = WM_RBUTTONUP) or (message = WM_CONTEXTMENU); end; end; //替換右鍵菜單 procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); var mPoint: TPoint; begin if IsChild(WebBrowser1.Handle, Msg.Hwnd) and ((Msg.Message = WM_RBUTTONDOWN) or (Msg.Message = WM_RBUTTONUP)) then begin GetCursorPos(mPoint); //得到光標(biāo)位置 PopupMenu1.Popup(mPoint.X, mPoint.Y); //彈出popupmenu1的菜單 Handled:=True; end; end; //新頁面寫入 begin WebBrowser1.Navigate(''about:blank''); WebBrowser1.OleObject.Document.Writeln(''ok''); end; //從流中寫入: var ms: TMemoryStream; begin ms := TMemoryStream.Create; Memo1.Lines.SaveToStream(ms); ms.Position := 0; (WebBrowser1.Document as IPersistStreamInit).Load (TStreamAdapter.Create(ms)); ms.Free; end; //禁止提示腳步錯(cuò)誤 procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant); begin WebBrowser1.Silent := True; end; //禁止彈出窗口 procedure TForm1.WebBrowser1NewWindow2(ASender: TObject; var ppDisp: IDispatch; var Cancel: WordBool); begin Cancel := True; end; 模擬點(diǎn)擊網(wǎng)頁中的按鈕 操作鏈接 |
|