Click here to Skip to main content
15,891,529 members
Articles / Desktop Programming / Windows Forms

Web Browser Link Trapping

Rate me:
Please Sign up or sign in to vote.
3.76/5 (10 votes)
27 Sep 2006CPOL2 min read 72.7K   754   21   16
How-to find out which link was clicked in a web browser control.

Sample Image - screenshot.gif

Introduction

I'm new to programming, and was trying to create a browser (using the web browser control) that connected through a TCP client. I soon realized that I would need to trap the links on the page, so I could redirect them through my application. A little bit of time and coffee later, I realized that no one appears to have ever needed this. Or at the very least, I was unable to find any information on it. However, I eventually gave up and decided to give it a shot myself. I am trying to get better with my programming, and would really like to try and make a career out of it, so please comment on this and let me know what you think or where I can improve.

Description of the solution

The web browser control shipped with the .NET Framework provides many useful functions and properties. As well as this, many of the events are available without too much fiddling around. First off, I monitored the DocumentCompleted event, and then made any changes to the document that I required. In this case, I used a HTMLElementCollection object to store all the links on the page. I then created an event handler, LinkClicked, and assigned it to the OnClick event of all links. This way, I was aware of any navigation that was occurring. I'll explain the code first.

Document Completed

Setting up the event handler is as simple as this:

VB
Private Sub webControl_DocumentCompleted(ByVal sender As Object, _ 
        ByVal e As WebBrowserDocumentCompletedEventArgs) _
        Handles webControl.DocumentCompleted
    Dim link As HtmlElement
    Dim links As HtmlElementCollection = webControl.Document.Links
    For Each link In links
        If btnChange.Checked Then
            link.SetAttribute("href", newLink)
        End If 

        link.AttachEventHandler("onclick", AddressOf LinkClicked)
    Next
End Sub

This code gets all the links on the page, and then assigns the LinkClicked event handler to the 'OnClick' event of those links.

Link Clicked

Here's the LinkClicked event:

VB
Private Sub LinkClicked(ByVal sender As Object, ByVal e As EventArgs) 
    Dim link As HtmlElement = webControl.Document.ActiveElement
    Dim url As String = link.GetAttribute("href") 
    MsgBox("Link Clicked: " & link.InnerText & _
            vbCrLf & "Destination: " & url)
End Sub

Now we know that a link was clicked. So next, I simply grab the document's currently active item (the link) and read in the 'href' string.

Additional Features

Cancel Navigation

The Navigating event occurs before the page is loaded, so it's the best place to cancel any navigation we don't want to occur:

VB
Private Sub webControl_Navigating(ByVal sender As Object, _ 
        ByVal e As WebBrowserNavigatingEventArgs) _
        Handles webControl.Navigating
    If btnCancel.Checked Then
        e.Cancel = True
    End If 
End Sub

Conclusion

The best thing about this solution is it involves only managed code. You get a managed event for any JavaScript event that occurs. There are many other properties and ideas that can be exploited here, but I think that's pretty much self explanatory. I hope this code is useful to someone; if not, at least, I finally got to post something on here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer uSoftware
Australia Australia
Shaps currently works as the Senior Networks Administrator for a small IT company in Australia. He is also involved in various software developments, for Windows, Windows Mobile, iPhone, Mac and occasionally the web. His central focus in this area, is User Interface design.

If he had spare time, he'd play some tunes, read a book or visit the gym. Until then, he writes small applications for the iPhone that help improve his productivity at work.

Comments and Discussions

 
QuestionIn case you want to open links in user's default browser Pin
cbishop198621-Dec-14 19:39
cbishop198621-Dec-14 19:39 
QuestionNice! Pin
cbishop198621-Dec-14 13:07
cbishop198621-Dec-14 13:07 
GeneralGood Job ! Pin
kasbaba14-Feb-09 5:02
kasbaba14-Feb-09 5:02 
QuestionHow to Trap all Redirecting URLs Pin
Sandeep Saini SRE16-Sep-08 20:31
Sandeep Saini SRE16-Sep-08 20:31 
GeneralBRILLIANT !! Pin
markinro8-Sep-08 16:14
markinro8-Sep-08 16:14 
GeneralRe: BRILLIANT !! Pin
Shahpour8-Sep-08 22:55
Shahpour8-Sep-08 22:55 
GeneralAddEventHandler not needed Pin
risingfish29-Aug-07 4:34
risingfish29-Aug-07 4:34 
AnswerRe: AddEventHandler not needed Pin
Shahpour29-Aug-07 4:54
Shahpour29-Aug-07 4:54 
GeneralVS.2003 Pin
celoftis15-May-07 9:21
celoftis15-May-07 9:21 
GeneralRe: VS.2003 Pin
dartrax13-Aug-07 8:06
dartrax13-Aug-07 8:06 
GeneralFrames & IFrames Pin
el_MKay31-Jan-07 7:55
el_MKay31-Jan-07 7:55 
GeneralRe: Frames & IFrames Pin
Shahpour29-Aug-07 4:59
Shahpour29-Aug-07 4:59 
GeneralVery Nice. Pin
plaguethenet25-Jan-07 11:55
plaguethenet25-Jan-07 11:55 
QuestionSource & Demo ??? Pin
fwsouthern27-Sep-06 10:56
fwsouthern27-Sep-06 10:56 
AnswerRe: Source & Demo ??? Pin
Shahpour27-Sep-06 19:58
Shahpour27-Sep-06 19:58 
GeneralonClick event can stop navigation [modified] Pin
The_Mega_ZZTer17-Sep-06 4:50
The_Mega_ZZTer17-Sep-06 4:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.