简体   繁体   English

将Webbrowser控件与ie7和锚一起使用时出现问题

[英]Problems using webbrowser control with ie7 and anchors

I'm seeing some very strange behavior when using ac# webbrowser control and a link with anchors in it. 使用ac#webbrowser控件及其中带有锚点的链接时,我看到一些非常奇怪的行为。 Oh and this behavior only seems to occur in ie7 in ie8 it's fine! 哦,这种行为似乎只发生在ie8的ie7中,这很好!

I've created a simple form with a webbrowser control. 我用web浏览器控件创建了一个简单的表单。

I've added a new WebBrowserNavigatedEventHandler to load in some html after the browser has loaded the first page. 我添加了一个新的WebBrowserNavigatedEventHandler,以在浏览器加载第一页后加载某些html。 A straight link to Google works fine, but a link to a wikipedia page and anchor point does nothing. 指向Google的直接链接可以正常工作,但指向Wikipedia页面和定位点的链接则无济于事。

If however I navigate to a page with anchors in it (or links to another page with links it works fine)! 但是,如果我导航到其中具有锚点的页面(或指向具有链接的其他页面的链接,则效果很好)!

So the issues may be with the page I'm loading the first time/how I'm loading it. 因此问题可能出在我第一次加载的页面/如何加载页面上。

Here is the code I've written, any suggestions would be appreciated. 这是我编写的代码,任何建议将不胜感激。

Edit: 编辑:

I've just noticed that if I change the line 我刚刚注意到,如果我换线

this.webBrowser1.Document.Write(html);

to: 至:

this.webBrowser1.DocumentText = html;

it works fine!!! 它工作正常!!!

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.webBrowser1.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_NavigatedLoaddefaultpage);
        this.webBrowser1.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_NavigatedUpdateTextbox);

        this.webBrowser1.Navigate("about:blank");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.webBrowser1.Navigate(this.textBox1.Text);
    }


    void webBrowser1_NavigatedLoaddefaultpage(object sender, WebBrowserNavigatedEventArgs e)
    {
        this.webBrowser1.Navigated -= new WebBrowserNavigatedEventHandler(webBrowser1_NavigatedLoaddefaultpage);

        string html = "<html>";
        html += "<body>";

        html += "<h1>My First Heading</h1>";

        html += "<p>My first paragraph.</p>";

        html += "<a href='http://en.wikipedia.org/wiki/Star_Trek#Production_history'>Star Trek Production history</a>";
        html += "Go to <a href='http://www.google.com'>Google Search</a><br />";
        html += "</body>";
        html += "</html>";
        this.webBrowser1.Document.Write(html);
    }

    void webBrowser1_NavigatedUpdateTextbox(object sender, WebBrowserNavigatedEventArgs e)
    {
        this.textBox1.Text = this.webBrowser1.Url.ToString();
    }
}

This may not even be a problem with your code. 这甚至可能与您的代码无关。 It could be an anchor bug that IE7 has. 可能是IE7的锚定错误。 I've been having issues with anchors not working in IE7 as well. 我一直遇到锚无法在IE7中正常工作的问题。 Apparently sometimes this happens and sometimes it doesn't. 显然有时这会发生,有时却不会。

I seem to have just run into this issue as well. 我似乎也刚遇到这个问题。 The webbrowser control refuses to load when I have a URL that includes an anchor (eg, http://cnn.com/#foo ). 当我有一个包含锚点的URL时,webbrowser控件拒绝加载(例如, http : //cnn.com/#foo )。 I'd swear this was working but I think Microsoft must have re-introduced this bug, assuming they had previously fixed it (or perhaps my test system just hadn't gotten the updates that included this bug). 我发誓这是可行的,但我认为微软一定已经重新引入了该错误,假设他们以前已经修复了该错误(或者我的测试系统只是没有得到包含此错误的更新)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM