Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Hyperlink Value

Hyperlink Value

Scheduled Pinned Locked Moved C#
csharptutorial
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    myousufq
    wrote on last edited by
    #1

    Could any one tell me that How to get the value of hyperlink in web browser by C#.

    H 1 Reply Last reply
    0
    • M myousufq

      Could any one tell me that How to get the value of hyperlink in web browser by C#.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You need to walk the DOM (document object model) by casting the AxWebBrowser2.Document property (or whatever the class is called, depending on how you imported it) to an IHTMLDocument3, which is defined in the Microsoft.mshtml.dll interop assembly, or you can always import your own using VS.NET or tlbimp.exE. Once you do that you simply walk the DOM like you would in HTML, or get the A link using it's ID (if set) like so:

      IHTMLDocument3 doc = (IHTMLDocument3)axWebBrowser2.Document;
      if (doc != null)
      {
      IHTMLAnchorElement a = doc.getElementById("myLink") as IHTMLAnchorElement;
      if (a != null)
      {
      Console.WriteLine(a.href);
      }
      }

      Search for "IHTMLDocument" in this forum by clicking "Search comments" for additional examples of how to walk the DOM using the Internet Explorer WebBrowser control. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

      M 1 Reply Last reply
      0
      • H Heath Stewart

        You need to walk the DOM (document object model) by casting the AxWebBrowser2.Document property (or whatever the class is called, depending on how you imported it) to an IHTMLDocument3, which is defined in the Microsoft.mshtml.dll interop assembly, or you can always import your own using VS.NET or tlbimp.exE. Once you do that you simply walk the DOM like you would in HTML, or get the A link using it's ID (if set) like so:

        IHTMLDocument3 doc = (IHTMLDocument3)axWebBrowser2.Document;
        if (doc != null)
        {
        IHTMLAnchorElement a = doc.getElementById("myLink") as IHTMLAnchorElement;
        if (a != null)
        {
        Console.WriteLine(a.href);
        }
        }

        Search for "IHTMLDocument" in this forum by clicking "Search comments" for additional examples of how to walk the DOM using the Internet Explorer WebBrowser control. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

        M Offline
        M Offline
        myousufq
        wrote on last edited by
        #3

        Thanks Heath on your reply . Basically I need to get the value of hyperlink on cursor like in web browser when we Hover the hyperlink, it showing hyperlink value in status bar.so the value which shows in status bar i need to get these values. please give me the solution in dotnet (C#)

        H 2 Replies Last reply
        0
        • M myousufq

          Thanks Heath on your reply . Basically I need to get the value of hyperlink on cursor like in web browser when we Hover the hyperlink, it showing hyperlink value in status bar.so the value which shows in status bar i need to get these values. please give me the solution in dotnet (C#)

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          I did give you a solution using C#, but it requires you to interop with COM. The solution gets the actual value of the A.href attribute. If you want an absolute URL, combine the IHTMLDocument2.location.href (casting AxWebBrowser2.Document to IHTMLDocument2 this time - this is just how COM interop and COM itself works) and the link value from the first solution:

          IHTMLDocument2 doc2 = (IHTMLDocument2)axWebBrowser2.Document;
          if (doc != null)
          {
          Uri url = new Uri(doc2.location.href);
          url = new Uri(url, a.href);
          }

          Now you've got an absolute URL. To be 100% correct, however, you should retrieve the IHTMLBaseElement from the <HEAD> and gets it's href attribute value - if any. This is an optional element so it may not exist; if it does exist it re-scopes URLs contained within the document to be relative to the <BASE href> value. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

          1 Reply Last reply
          0
          • M myousufq

            Thanks Heath on your reply . Basically I need to get the value of hyperlink on cursor like in web browser when we Hover the hyperlink, it showing hyperlink value in status bar.so the value which shows in status bar i need to get these values. please give me the solution in dotnet (C#)

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            ...and for more information about COM interoperability to help you walk the DOM using the WebBrowser control, read Exposing COM Components to the .NET Framework[^] in the .NET Framework SDK. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

            M 1 Reply Last reply
            0
            • H Heath Stewart

              ...and for more information about COM interoperability to help you walk the DOM using the WebBrowser control, read Exposing COM Components to the .NET Framework[^] in the .NET Framework SDK. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

              M Offline
              M Offline
              myousufq
              wrote on last edited by
              #6

              Thank you Heath for your prompt replies and usefull guidance...can you tell me what i should to make my browser to take the value of links when ever my mouse hovers it...because i have tried the way you have told but it is not picking the url of that link.

              H 1 Reply Last reply
              0
              • M myousufq

                Thank you Heath for your prompt replies and usefull guidance...can you tell me what i should to make my browser to take the value of links when ever my mouse hovers it...because i have tried the way you have told but it is not picking the url of that link.

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #7

                You need to handle the IHTMLDocument2.body's onmouseover event in order to get the current element under the mouse. Copy the code snippet and see how it works in the browser. Everything JScript can do your application can do because they use the same automation server - the WebBrowser control or MSHTML (which is hosted by the WebBrowser control):

                <html>
                <head>
                <title>Test</title>
                </head>
                <body onmouseover="showCurrentElement(event)">
                <p>This is a paragraph of text with a <a href="http://www.microsoft.com">hyperlink</a> embedded.</p>
                <p><b>Look at me - I'm bold!</b></p>
                <ul>
                <li>Item 1</li>
                <li>Item 2</li>
                <li>Item 3</li>
                </ul>
                <p id="currentElement"></p>
                </body>
                <script type="text/jscript">
                function showCurrentElement(e)
                {
                var lbl = document.getElementById("currentElement");
                if (lbl)
                {
                lbl.innerText = e.srcElement.tagName;
                }
                }
                </script>
                </html>

                You really need to read the links I gave you regarding COM interop. There's also several articles on this site about programming with the WebBrowser control, as well as on MSDN[^]. There's a lot to understand, but it's not difficult especially if you understand COM. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

                M 1 Reply Last reply
                0
                • H Heath Stewart

                  You need to handle the IHTMLDocument2.body's onmouseover event in order to get the current element under the mouse. Copy the code snippet and see how it works in the browser. Everything JScript can do your application can do because they use the same automation server - the WebBrowser control or MSHTML (which is hosted by the WebBrowser control):

                  <html>
                  <head>
                  <title>Test</title>
                  </head>
                  <body onmouseover="showCurrentElement(event)">
                  <p>This is a paragraph of text with a <a href="http://www.microsoft.com">hyperlink</a> embedded.</p>
                  <p><b>Look at me - I'm bold!</b></p>
                  <ul>
                  <li>Item 1</li>
                  <li>Item 2</li>
                  <li>Item 3</li>
                  </ul>
                  <p id="currentElement"></p>
                  </body>
                  <script type="text/jscript">
                  function showCurrentElement(e)
                  {
                  var lbl = document.getElementById("currentElement");
                  if (lbl)
                  {
                  lbl.innerText = e.srcElement.tagName;
                  }
                  }
                  </script>
                  </html>

                  You really need to read the links I gave you regarding COM interop. There's also several articles on this site about programming with the WebBrowser control, as well as on MSDN[^]. There's a lot to understand, but it's not difficult especially if you understand COM. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

                  M Offline
                  M Offline
                  myousufq
                  wrote on last edited by
                  #8

                  you mean that i should add this code snippet in my C# program?

                  H 1 Reply Last reply
                  0
                  • M myousufq

                    you mean that i should add this code snippet in my C# program?

                    H Offline
                    H Offline
                    Heath Stewart
                    wrote on last edited by
                    #9

                    No, you should do something like the HTML - which is what I said in my previous post - and handle the IHTMLDocument2.body.onmouseover event like you would any other event. You get the srcElement (the element that fired the event) from the IHTMLEventObj. The HTML was - again - an example of what you need to do. I've given you plenty of samples and you can find more by clicking "Search comments" to search this message board (and others, if you like) or use the search box at the top of every page to search for articles. If you read the documentation I've linked for you all this will make a lot more sense. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

                    M 1 Reply Last reply
                    0
                    • H Heath Stewart

                      No, you should do something like the HTML - which is what I said in my previous post - and handle the IHTMLDocument2.body.onmouseover event like you would any other event. You get the srcElement (the element that fired the event) from the IHTMLEventObj. The HTML was - again - an example of what you need to do. I've given you plenty of samples and you can find more by clicking "Search comments" to search this message board (and others, if you like) or use the search box at the top of every page to search for articles. If you read the documentation I've linked for you all this will make a lot more sense. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

                      M Offline
                      M Offline
                      myousufq
                      wrote on last edited by
                      #10

                      Thank You so much Heath....you have solved my problem.:)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups