WebBrowser Control / Restricting sites

munkee

Active member
Joined
Feb 20, 2007
Messages
43
Programming Experience
Beginner
Is there a way to only allow certain domains to be used within my WebBrowser control?


For instance, I only want it to use "domain1.com" AND "domain2.net" within the browser control.

If user tries to click a hyperlink that leaves those specified domains, then it will not go.


Does that make sense? If so, could you please nudge me in the right direction?


Thanks folks.
 
Check the e.Url.Host of Navigating event, set e.Cancel=True to stop the navigation. Example that keeps within msn.com domain, notice Navigating also may fire several time for a page to load elements and that url.host is empty string, investigate this.
VB.NET:
Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) _
Handles WebBrowser1.Navigating
    If Not (e.Url.Host.EndsWith("msn.com") Or e.Url.Host = "") Then
        e.Cancel = True
    End If
End Sub
 
that works great. :)

I'm trying to make it for multiple domains though.


obviously i'm not doing it right... but i've tried:

VB.NET:
If Not (Navigating.EndsWith ("url1.com", "url2.com") Or Navigating = "") Then
   e.Cancel = True
End If


reason being.. is that the main WebBrowser URL uses iFrames.
 
String.EndsWith function only takes one input string paramter and returns the Boolean value, so you need more of those ...
VB.NET:
if not (urhost="www.this.com" [B][COLOR=darkgreen]OR[/COLOR][/B] urlhost="www.that.com" [B][COLOR=#006400]OR[/COLOR][/B] urlhost ="")
 
Back
Top