Hyperlink Value
-
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 anIHTMLDocument3
, 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]
-
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 anIHTMLDocument3
, 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]
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#)
-
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#)
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 theIHTMLDocument2.location.href
(castingAxWebBrowser2.Document
toIHTMLDocument2
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'shref
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] -
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#)
...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]
-
...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]
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.
-
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.
You need to handle the
IHTMLDocument2.body
'sonmouseover
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]
-
You need to handle the
IHTMLDocument2.body
'sonmouseover
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]
-
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 thesrcElement
(the element that fired the event) from theIHTMLEventObj
. 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] -
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 thesrcElement
(the element that fired the event) from theIHTMLEventObj
. 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]