简体   繁体   中英

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. Oh and this behavior only seems to occur in ie7 in ie8 it's fine!

I've created a simple form with a webbrowser control.

I've added a new WebBrowserNavigatedEventHandler to load in some html after the browser has loaded the first page. A straight link to Google works fine, but a link to a wikipedia page and anchor point does nothing.

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. I've been having issues with anchors not working in IE7 as well. 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 ). 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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