Answered by:
Windows Forms Web Browser App, how to intercept a click on a webpage Link ?

Question
-
Hello & Thanks :
win 10 , Visual Basic , Forms WebBrowser app :
When a user clicks on a webPage Link , how can I intercept that Event .
What is the name of that Event ?
I want to check it against a list of badLinks ?
Thanks for your Help !
"All things in moderation, except for love and forgiveness."...vm
- Moved by Sara LiuMicrosoft contingent staff Monday, August 3, 2020 8:51 AM
Sunday, August 2, 2020 3:39 PM
Answers
-
I think that the fact of opening new window cannot be always determined from the value of target attribute. (For example, a new browser instance can be created with “Open link in new window” context menu. Or the target attribute could mean a frame in current browser).
To cancel unwanted requests and redirect them to current browser control, try handling the NewWindow event, setting Cancel to true and navigating to target URL:
- https://social.technet.microsoft.com/wiki/contents/articles/22943.preventing-external-links-from-opening-in-new-window-in-wpf-web-browser.aspx („Windows Forms Solution”; can be easily implemented in Visual Basic too)
By the way, you can also consider the modern WebView2 control: https://docs.microsoft.com/en-us/microsoft-edge/webview2/.- Edited by Viorel_MVP Monday, August 3, 2020 2:07 PM
- Marked as answer by vmars316 Tuesday, August 4, 2020 9:15 PM
Monday, August 3, 2020 2:02 PM
All replies
-
Consider the Navigating event. It also works with keyboard navigation, not only with mouse clicks. The Url and Cancel properties can be used to stop navigation to some addresses: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.webbrowsernavigatingeventargs.
Sunday, August 2, 2020 7:32 PM -
Oh that's Great news , Thanks ;
The other thing I want to detect if the link has a " target="_blank" in it . If the link is allowable , then I want to open it in the same Browser instance , not open a new window .
Can I do this ? I think it means having access to the whole Link in the html .
Thanks for your Help !
"All things in moderation, except for love and forgiveness."...vm
Monday, August 3, 2020 12:41 AM -
If I were to do this in JavaScript in document ready which works.
Array.prototype.forEach.call(document.getElementsByTagName("a"), function (el) { if (el.target === "_blank") { el.target = "_self"; } });
In VB.NET the following is untested but will work or need some modifications.
Public Class CodeSampleForm Private Sub WebBrowser1_DocumentCompleted( sender As Object, e As WebBrowserDocumentCompletedEventArgs) _ Handles WebBrowser1.DocumentCompleted Dim tags As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a") If tags.Count > 0 Then For Each item As HtmlElement In tags Dim targetValue = item.GetAttribute("target") If targetValue = "_blank" Then item.SetAttribute("", "_self") End If Next End If 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.
Monday, August 3, 2020 12:49 PM -
I think that the fact of opening new window cannot be always determined from the value of target attribute. (For example, a new browser instance can be created with “Open link in new window” context menu. Or the target attribute could mean a frame in current browser).
To cancel unwanted requests and redirect them to current browser control, try handling the NewWindow event, setting Cancel to true and navigating to target URL:
- https://social.technet.microsoft.com/wiki/contents/articles/22943.preventing-external-links-from-opening-in-new-window-in-wpf-web-browser.aspx („Windows Forms Solution”; can be easily implemented in Visual Basic too)
By the way, you can also consider the modern WebView2 control: https://docs.microsoft.com/en-us/microsoft-edge/webview2/.- Edited by Viorel_MVP Monday, August 3, 2020 2:07 PM
- Marked as answer by vmars316 Tuesday, August 4, 2020 9:15 PM
Monday, August 3, 2020 2:02 PM -
Thanks Karen ;
Does "Mark as answer" mean 'mark as an answer' or 'mark as THE answer' .
I don't want the system to close out the Thread because it thinks I'm all done .
Thanks
"All things in moderation, except for love and forgiveness."...vm
Monday, August 3, 2020 3:49 PM -
Thanks Karen ;
Does "Mark as answer" mean 'mark as an answer' or 'mark as THE answer' .
I don't want the system to close out the Thread because it thinks I'm all done .
Thanks
"All things in moderation, except for love and forgiveness."...vm
Hello,
"Mark as answer" can mean a) the answer b) there can be more than one answer so in that case one of the answers c) a reply was not a copy and paste answer but lead you to the answer.
So let's say I was the sole answer, you would mark me as so but if both myself and Viorel_ provided information then we would both be marked as answers.
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.
Monday, August 3, 2020 8:26 PM -
Hi vmars316,
Regarding your question, Viorel_ and Kareninstructor provide a good solution. And we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
Best Regards,
Daniel ZhangMSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Tuesday, August 4, 2020 1:40 AM