[英]Local path to JavaScript file are not working under loaded to WebBrowser control local HTML page
[英]Open local html file from link in page loaded in WebBrowser
我有一個WebBrowser控件,用於顯示具有不同信息(包括html鏈接)的動態生成的html頁面。 使用webBrowser.DocumentText加載頁面。
如果我包含指向外部網頁的鏈接,例如“ https://somesite.com/file.htm ”,則該鏈接可以正常工作,但是如果我鏈接到本地文件,例如“ file:/// c:\\ temp \\ file.htm”,則單擊鏈接不會發生任何反應。
如果我從WebBrowser控件的頁面中獲取源代碼,將其另存為html文件並在Edge中打開,則鏈接可以正常工作。
我瀏覽了無數網頁,試圖找到解決方案,但是沒有運氣,它們似乎都沒有解決這個我覺得很奇怪的特定問題,因為這似乎是一個常見問題。
string htmlCode = "<html><body><a href=\"file:///c:\\temp\\testlink.htm\">link</a></body></html>";
webBrowser1.DocumentText = htmlCode;
如果您提供了一個可以重現您所遇到的問題的示例,則將更加有用,但這是解決問題的一種方法。
下面的代碼編寫了兩個文件: start.html
,它包含指向第二個文件end.html
的可單擊鏈接, end.html
僅僅包含一些文本。
將一個Web瀏覽器控件放到窗體上並運行代碼。 第一頁將加載您可以單擊的鏈接,單擊該鏈接時,第二頁將加載。
public Form1()
{
InitializeComponent();
// Modify these with paths on your machine
var startPage = @"f:\private\temp\start.html";
var endPage = @"f:\private\temp\end.html";
// This will write the contents of the files above (so they exist)
File.WriteAllText(startPage, $"<a href=\"file:///{endPage}\">Click Here</a>");
File.WriteAllText(endPage, "You did it!!");
// Navigate to the first file so you can click the link
webBrowser1.Navigate(startPage);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.