WebBrowser operation in Delphi
WebBrowser1.GoHome; // Go to browser default home page WebBrowser1.Refresh; // Refresh WebBrowser1.GoBack; // Back WebBrowser1.GoForward; // forward WebBrowser1.Navigate ('...'); // Open the specified page WebBrowser1.Navigate ('about: blank'); // Open empty page
// Open the empty page and write ... WebBrowser1.Navigate ('about: <head> <title> Title </ title> <body> Page content </ body>');
// Read the variables in the web script: 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 // can also be read like this: s: = WebBrowser1.OleObject.document.parentWindow.str; i: = WebBrowser1.OleObject.document.parentWindow.num; ShowMessage (s); // Hello ShowMessage (IntToStr (i)); // 99 end ;
If there is such a statement in the web page:
// Call the function in the web script: procedure TForm1.Button1Click (Sender: TObject); begin WebBrowser1.OleObject.document.parentWindow.MB (); // HTML-Js // To specify a scripting language, you need: WebBrowser1.OleObject.document.parentWindow.execScript ('MB ()','JavaScript'); // HTML-Js end ;
If there is such a script:
// Judge whether all the web pages and internal frame web pages have been downloaded procedure TForm1.WebBrowser1DocumentComplete (ASender: TObject; const pDisp: IDispatch;var URL: OleVariant); begin if WebBrowser1.Application = pDispthen begin Text: ='The web page is downloaded! '; end ; end ;
// Change the background color or background image: WebBrowser1.OleObject.document.body.bgcolor: = '# FF0000'; WebBrowser1.OleObject.document.body.background: = '...The map's address';
// Manipulate objects with ID tags: var s:string ; begin s: = WebBrowser1.OleObject.document.getElementById ('span1') .innerText; ShowMessage (s); // This is the content in the span1 tag //or: s: = WebBrowser1.OleObject.document.parentWindow.span1.innerText; ShowMessage (s); // This is the content in the span1 tag // Hide it: WebBrowser1.OleObject.document.parentWindow.span1.style.display: = 'none'; end ;
If the webpage has this content: <span id = span1> This is the content in the span1 tag </ span>
// Get web page source code var s:string ; begin s: = WebBrowser1.OleObject.document.body.innerHTML; // all code inside body s: = WebBrowser1.OleObject.document.body.outerHTML; // all code inside body, including body tag s: = WebBrowser1.OleObject.document.documentElement.innerHTML; // all code inside html end ; // Get all source code of the web page 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 and other double bytes} ms.Free; end ;
// Right-click menu in WebBrowser // First add ApplicationEvents1 and specify its Message event // Shield right-click menu procedure TForm1.ApplicationEvents1Message (var Msg: tagMSG;var Handled: Boolean); begin with Msgdo begin if not IsChild (WebBrowser1.Handle, hWnd)then Exit; Handled: = (message = WM_RBUTTONDOWN)or (message = WM_RBUTTONUP)or (message = WM_CONTEXTMENU); end ; end ; // Replace the right-click menu 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); // Get cursor position PopupMenu1.Popup (mPoint.X, mPoint.Y); // popup menu of popupmenu1 Handled: = True; end ; end ;
// New page write begin WebBrowser1.Navigate ('about: blank'); WebBrowser1.OleObject.Document.Writeln ('ok'); end ; // Write from stream: 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 ; // Prohibit prompting footstep errors procedure TForm1.WebBrowser1NavigateComplete2 (ASender: TObject; const pDisp: IDispatch;var URL: OleVariant); begin WebBrowser1.Silent: = True; end ; // Suppress pop-up windows procedure TForm1.WebBrowser1NewWindow2 (ASender: TObject;var ppDisp: IDispatch; var Cancel: WordBool); begin Cancel: = True; end ;
Support high version ie:
<meta http-equiv = “X-UA-Compatible” content = “IE = edge” />