Board index » delphi » How to redirect the webpage by TwebBrowser

How to redirect the webpage by TwebBrowser


2003-09-02 10:27:51 AM
delphi176
I use a TWebBrowser to navigate a web page 127.0.0.1/index.html,
The following are the codes for index.html:
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 2</title>
</head>
<body>
<p>. <a href="new_page_2.htm">the first site</a></p>
<p>. the second site</p>
<p>. the third site</p>
</body>
</html>
Now I want the Twebbrowser to automatically click the first site,how to
achieve that by codes?
 
 

Re:How to redirect the webpage by TwebBrowser

"floweasy" <XXXX@XXXXX.COM>schrieb im Newsbeitrag
Quote
I use a TWebBrowser to navigate a web page 127.0.0.1/index.html,
The following are the codes for index.html:

<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 2</title>
</head>

<body>

<p>. <a href="new_page_2.htm">the first site</a></p>
<p>. the second site</p>
<p>. the third site</p>

</body>

</html>


Now I want the Twebbrowser to automatically click the first site,how to
achieve that by codes?
Here is a code snippet showing how to get all links:
procedure TFrmHTMLViewer.AddLinks;
var
doc, links, link : OleVariant;
i : Integer;
begin
if not Assigned(WebBrowser1.Document) then
Exit; // no document loaded
doc := WebBrowser1.Document;
links := doc.links;
// loop through all links
for i:=0 to links.Length-1 do
begin
link := links.item(i);
// add the link to a TMemo
Memo1.Lines.Add(link.HREF);
end;
end;
All you have to do now is to navigate to one of the extracted
links:
var
Myolevariant : olevariant;
MyTargetFrameName : olevariant;
MyPostaData : olevariant;
MyHeaders : olevariant;
begin
{ Flags:
Constant Value Meaning
NavOpenInNewWindow $01 Open the resource or file in a new window.
NavNoHistory $02 Do not add the resource or file to the history
list. The new page replaces the current page in the list.
NavNoReadFromCache $04 Do not read from the disk cache for this
navigation.
NavNoWriteToCache $08 Do not write the results of this navigation to the
disk cache.
NavAllowAutosearch $10 If the navigation fails, the Web browser attempts
to navigate common root domains (.com, .org, and so on). If this still
fails, the URL is passed to a search engine.
}
Myolevariant := $00;
MyTargetFrameName := null;
MyPostaData := null;
MyHeaders := null;
//checkBox1.Checked := true;
Webbrowser1.Navigate(Memo1.lines[0],
Myolevariant,MyTargetFrameName,MyPostaData,MyHeaders);
hth
Andreas Schmidt
 

Re:How to redirect the webpage by TwebBrowser

thanks.It works well!
"Andreas Schmidt" <XXXX@XXXXX.COM>????
Quote

"floweasy" <XXXX@XXXXX.COM>schrieb im Newsbeitrag
news:3f5402b9$XXXX@XXXXX.COM...
>I use a TWebBrowser to navigate a web page 127.0.0.1/index.html,
>The following are the codes for index.html:
>
><html>
>
><head>
><meta http-equiv="Content-Language" content="en-us">
><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
><meta name="GENERATOR" content="Microsoft FrontPage 4.0">
><meta name="ProgId" content="FrontPage.Editor.Document">
><title>New Page 2</title>
></head>
>
><body>
>
><p>. <a href="new_page_2.htm">the first site</a></p>
><p>. the second site</p>
><p>. the third site</p>
>
></body>
>
></html>
>
>
>Now I want the Twebbrowser to automatically click the first site,how to
>achieve that by codes?


Here is a code snippet showing how to get all links:



procedure TFrmHTMLViewer.AddLinks;
var
doc, links, link : OleVariant;
i : Integer;
begin
if not Assigned(WebBrowser1.Document) then
Exit; // no document loaded

doc := WebBrowser1.Document;

links := doc.links;

// loop through all links
for i:=0 to links.Length-1 do
begin
link := links.item(i);

// add the link to a TMemo
Memo1.Lines.Add(link.HREF);
end;
end;


All you have to do now is to navigate to one of the extracted
links:


var
Myolevariant : olevariant;
MyTargetFrameName : olevariant;
MyPostaData : olevariant;
MyHeaders : olevariant;
begin

{ Flags:
Constant Value Meaning
NavOpenInNewWindow $01 Open the resource or file in a new window.
NavNoHistory $02 Do not add the resource or file to the history
list. The new page replaces the current page in the list.
NavNoReadFromCache $04 Do not read from the disk cache for this
navigation.
NavNoWriteToCache $08 Do not write the results of this navigation to
the
disk cache.
NavAllowAutosearch $10 If the navigation fails, the Web browser attempts
to navigate common root domains (.com, .org, and so on). If this still
fails, the URL is passed to a search engine.
}


Myolevariant := $00;
MyTargetFrameName := null;
MyPostaData := null;
MyHeaders := null;
//checkBox1.Checked := true;
Webbrowser1.Navigate(Memo1.lines[0],
Myolevariant,MyTargetFrameName,MyPostaData,MyHeaders);

hth
Andreas Schmidt