如何始终在 TWebBrowser 中加载新页面?
How to always load a fresh page in TWebBrowser?
我正在尝试使用 Browser.Navigate(URL) 将网页加载到 TWebBrowser。但是,浏览器不会关心页面是否在线更新,所以只要我不重新启动程序,它就不会显示新页面。
一个更具体的例子:
如果我导航到具有访问者计数器(如图所示)的网页,计数器将增加。如果我离开该页面然后 return(不使用返回),计数器将不会递增。在 Firefox 中,它确实递增。
这是我试过但行不通的方法:
const
DLCTL_PRAGMA_NO_CACHE = [=10=]004000;
procedure TBrowserFrm.LoadURL(URL: string);
var
Flag: OleVariant;
begin
Flag:=DLCTL_PRAGMA_NO_CACHE;
Browser.Navigate(URL, Flag);
end;
procedure TBrowserFrm.LoadURL(URL: string);
var
Flags: OleVariant;
begin
Flags := 'navNoHistory, navNoReadFromCache, navNoWriteToCache';
Browser.navigate2(URL, Flags);
end;
知道如何让 TWebBrowser 加载真实页面吗?
在 VCL 中,TWebBrowser
是 Internet Explorer 的包装器,特别是 IWebBrowser2
接口。
DLCTL_PRAGMA_NO_CACHE
不是 可以传递给 Navigate2()
的标志。阅读文档:
TWebBrowser wraps the IWebBrowser2 interface from Microsoft's Shell Doc Object and Control Library (SHDOCVW.DLL) to allow you to create a customized Web browsing application or to add Internet, file and network browsing, document viewing, and data downloading capabilities to your applications.
IWebBrowser2::Navigate2 Method
Flags [in] A pointer to a VARIANT of type VT_I4 or VT_I2 that specifies a combination of the values defined by the BrowserNavConstants enumeration.
BrowserNavConstants Enumerated Type
typedef enum BrowserNavConstants {
navOpenInNewWindow = 0x1,
navNoHistory = 0x2,
navNoReadFromCache = 0x4,
navNoWriteToCache = 0x8,
navAllowAutosearch = 0x10,
navBrowserBar = 0x20,
navHyperlink = 0x40,
navEnforceRestricted = 0x80,
navNewWindowsManaged = 0x0100,
navUntrustedForDownload = 0x0200,
navTrustedForActiveX = 0x0400,
navOpenInNewTab = 0x0800,
navOpenInBackgroundTab = 0x1000,
navKeepWordWheelText = 0x2000,
navVirtualTab = 0x4000,
navBlockRedirectsXDomain = 0x8000,
navOpenNewForegroundTab = 0x10000
} BrowserNavConstants;
如您所见,DLCTL_PRAGMA_NO_CACHE
不在该列表中。它实际上是您在为浏览器的 DISPID_AMBIENT_DLCONTROL
属性 实现处理程序时指定为输出值的标志。阅读 MSDN 文档:
WebBrowser Customization | Controlling Download and Execution
The WebBrowser Control gives you control over what it downloads, displays, and executes. To gain this control, you need to implement your host's IDispatch so it handles DISPID_AMBIENT_DLCONTROL. When the WebBrowser Control is instantiated, it will call your IDispatch::Invoke with this ID. Set pvarResult to a combination of following flags, using the bitwise OR operator, to indicate your preferences.
...
•DLCTL_RESYNCHRONIZE and DLCTL_PRAGMA_NO_CACHE: These flags cause cache refreshes. With DLCTL_RESYNCHRONIZE, the server will be asked for update status. Cached files will be used if the server indicates that the cached information is up-to-date. With DLCTL_PRAGMA_NO_CACHE, files will be re-downloaded from the server regardless of the update status of the files.
...
因此您必须实现自定义 IDispatch
对象并将其挂接到 IWebBrowser2
才能正确使用 DLCTL_PRAGMA_NO_CACHE
。
或者,您可以考虑切换到 TEmbeddedWB
,它会为您处理浏览器定制,并且有一个 DownloadOptions
属性 接受 DLCTL...
标志,包括 DLCTL_PRAGMA_NO_CACHE
.
我想你必须使用 4,而不是 $00004000。
因为我使用www.ghisler.com(计数器在底部)我可以使用
procedure TForm2.Button2Click(Sender: TObject);
var
Flags: OLEVariant;
begin
Flags:=4; //NavNoReadFromCache
WebBrowser1.Navigate('http://www.ghisler.com/', Flags);
end;
并且它运行完美 (Delphi XE7)。我看到 TC 主页,单击超链接,然后再次单击 Button2,计数器是新的。当我仅使用 Navigate(URL) 时,计数器仍然相同。
Browser.EnableCaching:=假;
Browser.Navigate;
我正在尝试使用 Browser.Navigate(URL) 将网页加载到 TWebBrowser。但是,浏览器不会关心页面是否在线更新,所以只要我不重新启动程序,它就不会显示新页面。
一个更具体的例子: 如果我导航到具有访问者计数器(如图所示)的网页,计数器将增加。如果我离开该页面然后 return(不使用返回),计数器将不会递增。在 Firefox 中,它确实递增。
这是我试过但行不通的方法:
const
DLCTL_PRAGMA_NO_CACHE = [=10=]004000;
procedure TBrowserFrm.LoadURL(URL: string);
var
Flag: OleVariant;
begin
Flag:=DLCTL_PRAGMA_NO_CACHE;
Browser.Navigate(URL, Flag);
end;
procedure TBrowserFrm.LoadURL(URL: string);
var
Flags: OleVariant;
begin
Flags := 'navNoHistory, navNoReadFromCache, navNoWriteToCache';
Browser.navigate2(URL, Flags);
end;
知道如何让 TWebBrowser 加载真实页面吗?
在 VCL 中,TWebBrowser
是 Internet Explorer 的包装器,特别是 IWebBrowser2
接口。
DLCTL_PRAGMA_NO_CACHE
不是 可以传递给 Navigate2()
的标志。阅读文档:
TWebBrowser wraps the IWebBrowser2 interface from Microsoft's Shell Doc Object and Control Library (SHDOCVW.DLL) to allow you to create a customized Web browsing application or to add Internet, file and network browsing, document viewing, and data downloading capabilities to your applications.
IWebBrowser2::Navigate2 Method
Flags [in] A pointer to a VARIANT of type VT_I4 or VT_I2 that specifies a combination of the values defined by the BrowserNavConstants enumeration.
BrowserNavConstants Enumerated Type
typedef enum BrowserNavConstants {
navOpenInNewWindow = 0x1,
navNoHistory = 0x2,
navNoReadFromCache = 0x4,
navNoWriteToCache = 0x8,
navAllowAutosearch = 0x10,
navBrowserBar = 0x20,
navHyperlink = 0x40,
navEnforceRestricted = 0x80,
navNewWindowsManaged = 0x0100,
navUntrustedForDownload = 0x0200,
navTrustedForActiveX = 0x0400,
navOpenInNewTab = 0x0800,
navOpenInBackgroundTab = 0x1000,
navKeepWordWheelText = 0x2000,
navVirtualTab = 0x4000,
navBlockRedirectsXDomain = 0x8000,
navOpenNewForegroundTab = 0x10000
} BrowserNavConstants;
如您所见,DLCTL_PRAGMA_NO_CACHE
不在该列表中。它实际上是您在为浏览器的 DISPID_AMBIENT_DLCONTROL
属性 实现处理程序时指定为输出值的标志。阅读 MSDN 文档:
WebBrowser Customization | Controlling Download and Execution
The WebBrowser Control gives you control over what it downloads, displays, and executes. To gain this control, you need to implement your host's IDispatch so it handles DISPID_AMBIENT_DLCONTROL. When the WebBrowser Control is instantiated, it will call your IDispatch::Invoke with this ID. Set pvarResult to a combination of following flags, using the bitwise OR operator, to indicate your preferences.
...
•DLCTL_RESYNCHRONIZE and DLCTL_PRAGMA_NO_CACHE: These flags cause cache refreshes. With DLCTL_RESYNCHRONIZE, the server will be asked for update status. Cached files will be used if the server indicates that the cached information is up-to-date. With DLCTL_PRAGMA_NO_CACHE, files will be re-downloaded from the server regardless of the update status of the files.
...
因此您必须实现自定义 IDispatch
对象并将其挂接到 IWebBrowser2
才能正确使用 DLCTL_PRAGMA_NO_CACHE
。
或者,您可以考虑切换到 TEmbeddedWB
,它会为您处理浏览器定制,并且有一个 DownloadOptions
属性 接受 DLCTL...
标志,包括 DLCTL_PRAGMA_NO_CACHE
.
我想你必须使用 4,而不是 $00004000。
因为我使用www.ghisler.com(计数器在底部)我可以使用
procedure TForm2.Button2Click(Sender: TObject);
var
Flags: OLEVariant;
begin
Flags:=4; //NavNoReadFromCache
WebBrowser1.Navigate('http://www.ghisler.com/', Flags);
end;
并且它运行完美 (Delphi XE7)。我看到 TC 主页,单击超链接,然后再次单击 Button2,计数器是新的。当我仅使用 Navigate(URL) 时,计数器仍然相同。
Browser.EnableCaching:=假;
Browser.Navigate;