user1105748
user1105748

Reputation: 185

Gecko WebBrowser, getting the url from a selected hyperlink

This is a real 2%er but here goes, I have created a winform in VS2010 with Gecko 2.0.1-0.10 (latest release) webbrowser control, I am using a touch screen to navigate. Sometimes when clicking a hyperlink it will select text rather than navigate, on the DomMouseUp event I want to check to see if there is selected text, if so I want to see if it is a hyperlink and if it is, where that hyperlink goes to. I had a mess around with GeckoSelection but nothing looked obvious. I am looking for a way to see if what is selected within the web browser is a hyperlink, any thoughts?

Upvotes: 1

Views: 2772

Answers (3)

sh2dow
sh2dow

Reputation: 21

Very good behavior gives this call: m_strInnerHtml = geckoWebBrowser1.Url.AbsoluteUri()

Upvotes: 0

user1105748
user1105748

Reputation: 185

Update: An even better solution is to use (in DomFocus again)

m_strInnerHtml = geckoWebBrowser.Document.ActiveElement.GetAttribute("href");

This will return the actual hyperlink address, one thing to watch out for however is if you're on google for example and select the "Advertising" hyperlink at the bottom of the page, it may return "/advertisingpage/" which must be appended to the original url. Clicking a hyperlink away from google will return the full address however.

Upvotes: 0

user1105748
user1105748

Reputation: 185

I've worked it out : for anyone interested in such things, use DomFocus on the gecko control:

m_strInnerHtml = geckSel.ActiveElement.Parent.InnerHtml.ToString();

That will give you the innerhtml of the selected item, from here you can extract the href tag and get the url, navigate to it, hey presto :)

Upvotes: 2

Related Questions