locked
How to: get url Address from click on a webPage link ? RRS feed

  • Question

  • Hello & Thanks ;

    Windows Forms Browser app : 

    How soon after user clicks on a webPage link , 

    can I get the url Address that they clicked . 

    I want to check it against a list of 'SafeSites' .

    (for freeware kidSafeBrowser)

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Sunday, August 16, 2020 4:20 PM

Answers

  • i think in your code shoud be 
      if (e.Url.ToString().Contains("google.com"))

    instead of

    e.Url.ToString() == "https://www.google.com/"


    I did it that way from what I've seen as a web developer through the years.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by vmars316 Friday, August 28, 2020 4:08 AM
    Monday, August 17, 2020 4:41 PM

All replies

  • If I load with links to Google and Amazon with the intent to deny Google.

    <!DOCTYPE html>
    
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <p><a href="https://www.google.com/">Google</a></p>
        <p><a href="https://www.amazon.com/">Amazon</a></p>
    </body>
    </html>
    
    

    Load the page

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.DocumentText = File.ReadAllText("HTMLpage1.html");
        webBrowser1.Navigating += WebBrowser1_Navigating;
    }

    Check for Google and if so deny it.

    private void WebBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        if (e.Url.ToString() == "https://www.google.com/")
        {
            e.Cancel = true;
        }
    }

    Or perhaps

    private void WebBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        if (Regex.IsMatch(e.Url.Host, Regex.Escape("google"), RegexOptions.IgnoreCase))
        {
            e.Cancel = true;
        }
    }
    In a real app you would have a list of sites in a list to work from.

    Sunday, August 16, 2020 6:33 PM
  • i think in your code shoud be 
      if (e.Url.ToString().Contains("google.com"))

    instead of

    e.Url.ToString() == "https://www.google.com/"


    Monday, August 17, 2020 1:55 AM
  • Hello & Thanks ;

    My BAD , I should have said  Visual Basic .

    Windows Forms Browser app Visual Basic : 

    How soon after user clicks on a webPage link , 

    can I get the url Address that they clicked . 

    I want to check it against a list of 'SafeSites' .

    (for freeware kidSafeBrowser) 

    I guess this is the earliest then ? :

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

    No Hooks (or secret hand shakes) any time sooner ? 

    Thanks for your Help..


    "All things in moderation, except for love and forgiveness."...vm

    Monday, August 17, 2020 1:59 AM
  • Hi vmars316,
    First, you can use the Navigating event to handle when the site navigates to another url.
    Then you can use the WebBrowserNavigatingEventArgs to retrieve the new URL and to stop it if it is a denied link

    Private Sub webBrowser1_Navigating(ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs)
        'check you url and if so deny it.
        If e.Url.AbsoluteUri.Contains("something") Then
            e.Cancel = True
        Else
            'continue
        End If
    End Sub

    Best Regards,
    Daniel Zhang


    "Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Monday, August 17, 2020 2:46 AM
  • Hello & Thanks ;

    My BAD , I should have said  Visual Basic .

    Windows Forms Browser app Visual Basic : 

    How soon after user clicks on a webPage link , 

    can I get the url Address that they clicked . 

    I want to check it against a list of 'SafeSites' .

    (for freeware kidSafeBrowser) 

    I guess this is the earliest then ? :

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

    No Hooks (or secret hand shakes) any time sooner ? 

    Thanks for your Help..


    "All things in moderation, except for love and forgiveness."...vm

    Imports System.IO
    Imports System.Text.RegularExpressions
    
    Public Class Form1
        Private Sub WebBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs)
            If Regex.IsMatch(e.Url.Host, Regex.Escape("google"), RegexOptions.IgnoreCase) Then
                e.Cancel = True
            End If
        End Sub
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            WebBrowser1.DocumentText = File.ReadAllText("HTMLpage1.html")
            AddHandler WebBrowser1.Navigating, AddressOf WebBrowser1_Navigating
        End Sub
    
    End Class
    



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, August 17, 2020 12:27 PM
  • i think in your code shoud be 
      if (e.Url.ToString().Contains("google.com"))

    instead of

    e.Url.ToString() == "https://www.google.com/"


    I did it that way from what I've seen as a web developer through the years.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by vmars316 Friday, August 28, 2020 4:08 AM
    Monday, August 17, 2020 4:41 PM
  • Hi vmars316,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly. If not solved, what problem did you encounter?
    Best Regards,
    Daniel Zhang


    "Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Tuesday, August 18, 2020 9:15 AM