Making the <A NAME="..."> tag work with the WebBrowser

Hi.

My question is best illustrated with a code sample. Try the following: Create a form, and put a WebBrowser control and a button on the form. Double-click the button, and create the following code in the click event handler:

private void button1_Click(object sender, EventArgs e)
{
webBrowser1.DocumentText =
@"<html><body><a href=""about:blank#test2"">Test</a>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<a name=""test2"">Test2</a><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/> "
;
}

Run the form, and try to click on the "Test" link. You'll see that the navigation does not work correctly. Does anyone know of a way around this Please note that the requirements for this app does not permit storing html files on the computer's hard drive.

Thanks,

Eyvind.



Answer this question

Making the <A NAME="..."> tag work with the WebBrowser

  • Steve Clibbery

    it doesnt work because whn you click it reloads about:blank, but there is no html in the page after reload.

    this works -

    first add a COM reference to the Microsoft Html Object Library and try this code:

    Private WithEvents test1 As mshtml.HTMLAnchorElement
    Private test2 As mshtml.HTMLAnchorElement
    Private windowDom As mshtml.HTMLWindow2

    Private
    Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    WebBrowser1.Navigate(
    "about:blank")

    dim docDom As mshtml.HTMLDocument = WebBrowser1.Document.DomDocument
    dim bodyDom As mshtml.HTMLBody = docDom.createElement(
    "body")
    bodyDom.innerHTML =
    "<a id=""test1"" href="""" >Test</a> " + _
    "<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>" + _
    "<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>" + _
    "<br/><br/><a id=""test2"">Test2</a><br/><br/><br/><br/>" + _
    "<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>"
    docDom.appendChild(bodyDom)

    test1 = docDom.getElementById(
    "test1")
    test2 = docDom.getElementById(
    "test2")
    windowDom = docDom.parentWindow
    End
    Sub

    Private Function test1_onclick() As Boolean Handles test1.onclick
    windowDom.scrollBy(test2.offsetLeft - test1.offsetLeft, test2.offsetTop - test1.offsetTop)
    End Function



  • Kevin Wu

    Thanks anyway - any other ideas

    Eyvind.


  • Craig Z

    nevermind . . . doesnt work. . .sorry

  • PQuintas

    Yeah, I knew there had to be another way. I am really just beginning to feel my way around the DOM and the WebBrowser

  • Ahmad Jalal

    Hi Blair, and thank you very much for your reply. Your suggestion inspired me to find another solution to the problem, that does not involve mshtml.

    Basically, the idea is to get the element using the managed HTMLDocument.GetElementById method, and using the element's ScrollIntoView method to emulate navigation.

    Eyvind.


  • WolandIL

    try

    <a href="#test2">Test</a>



  • Making the <A NAME="..."> tag work with the WebBrowser