1. Get the variable value in the webpage
For example, in htm, <SCRIPT> var currids = 123 </SCRIPT>
The program can call ID: = form1.webbrowser1. oleobject. Document. Script. currids
Note: The variable can be defined in Javascript or VBScript. If the variable cannot be found in webbrowser1, an exception event is triggered, that is, the variable currids does not exist.
2. Execute functions on the webpage
Tmpf: = 'currid = getnextid (currid) '+ #13 #10;
Form1.webbrowser1.oleobject.document.parentwindow.exe cscript (tmpf, 'javascript ');
The execScript interface is used to call a function. Similarly, if the function does not exist or a running error occurs, a script error exception is triggered.
3. Set the webpage background
Background Image: webbrowser1.oleobject. Document. Body. Background: = 'HTTP: // seelearn.com/bg.gif ';
Background Color: webbrowser1.oleobject. Document. Body. bgcolor: = '# eeeeeee'
4. Call known objects in the webpage
Src: = WebBrowser1.OleObject. Document. getElementByID ('img1'). src
This method is actually getElementByID in javascript.
5. Retrieve all frames on the page
Use DHTML.
Frames: Invalid wb.oleobject.doc ument. frames;
For I: = 0 to frames. length do
Memo1.lines.add(frames[ I }.doc ument. body. innerHTML );
6. After BorderStyle = bsNone, Webbrowser will be reinitialized
This is a very surprising problem. Delphi has done a very good job in the control of window controls, and few such bugs have occurred.
According to the analysis, there are many situations where FormStyle is changed. If webbrowser. parent is changed from panel1 to panel2. webbrowser will also be reinitialized.
7. write html code directly to Webbrowser without navigating to an existing file.
Var
StrStream: TStringStream;
Setnotestr: string;
Begin
Setnotestr: = '<body bgcolor = 222222 align = center> <br> <p align = center> <font size = + 2 color = # ffffff> click blog http://seelearn.com </font> </ p> ';
Setnotestr: = setnotestr + '<br> <p align = center> <font size = + 2 color = # ffffff> click the button on the left to view the corresponding image </font> </P>';
Strstream: = tstringstream. Create (setnotestr );
Webbrowser1.navigate ('about: blank ');
Try
Strstream. Position: = 0;
(Webbrowser1.document as ipersiststreaminit). Load (tstreamadapter. Create (strstream ));
Finally
Strstream. Free;
End;
8. forward, backward, refresh
Self. webbrowser1.goback
Self. webbrowser1.goforward
Self. webbrowser1.refresh
9. Capture the newwindow2 event, that is, the new window event.
Procedure tform1.webbrowser1newwindow2 (Sender: tobject;
VaR ppdisp: idispatch; var cancel: wordbool );
VaR
Newwindow: tform2;
Begin
// Exit;
Newwindow: = tform2.create (NiL );
Newwindow. show;
Ppdisp: = newwindow. webbrowser1.defadisdispatch;
End;
It is worth noting that this method cannot obtain the URL of the new window. The method to exit is to wait until the beforenavigate2 event is triggered in the new webbrowser to determine
10. If an IFRAME exists in the webpage, determine whether the download is complete.
Procedure tform1.webbrowser1documentcomplete (Sender: tobject;
Const Pdisp: idispatch; var URL: olevariant );
Begin
If webbrowser1.application = Pdisp then showmessage ('all pages have been downloaded ')
End;
Note: after each IFRAME is downloaded, The documentcomplete event is triggered. Therefore, a page may be triggered multiple times before the download is completed.