5,442,984 members and growing! (18,447 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

Web Browser Link Trapping

By Shahpour

How-To find out which link was clicked in a web browser control.
VB, Windows, .NETVS.NET2003, VS2005, IE 6.0, IE 5.5, Visual Studio, IE, Dev

Posted: 16 Sep 2006
Updated: 27 Sep 2006
Views: 21,445
Bookmarked: 9 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 2.59 Rating: 3.07 out of 5
1 vote, 14.3%
1
1 vote, 14.3%
2
1 vote, 14.3%
3
2 votes, 28.6%
4
2 votes, 28.6%
5

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 shipping 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:

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:

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:

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Shahpour


Shaps currently works as the Senior Networks Administrator for a small IT company in Australia. He is also involved in various software developments, primarily for Windows Mobile devices. His central focus in this area, is User Interface design.

In his spare time he is currently creating blogging software for the PDA and music software for the PC.

He is also a musician with a love for Jazz and Blues.
Occupation: Web Developer
Location: Australia Australia

Other popular C / C++ Language articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralAddEventHandler not neededmemberrisingfish5:34 29 Aug '07  
AnswerRe: AddEventHandler not neededmemberShahpour5:54 29 Aug '07  
GeneralVS.2003memberceloftis10:21 15 May '07  
GeneralRe: VS.2003memberdartrax9:06 13 Aug '07  
GeneralFrames & IFramesmemberel_MKay8:55 31 Jan '07  
GeneralRe: Frames & IFramesmemberShahpour5:59 29 Aug '07  
GeneralVery Nice.memberplaguethenet12:55 25 Jan '07  
GeneralSource & Demo ???memberfwsouthern11:56 27 Sep '06  
AnswerRe: Source & Demo ???membershaps8020:58 27 Sep '06  
GeneralonClick event can stop navigation [modified]memberThe_Mega_ZZTer5:50 17 Sep '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Sep 2006
Editor: Smitha Vijayan
Copyright 2006 by Shahpour
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project