Trap anchor tags
Using CoreWebview2 to replace the IE ocx control, I have the need to trap when an anchor tag has been clicked on. I beleive in IE this was done using BeforeNavigate2 event but I do not see a direct replacement.
For example:
<a href='#Code123'>Run code 123 </a>
<a href='#Code124'>Run code 124 </a>
<a href='#Code125'>Run code 125 </a>
In this case these sections #Code123 doesn't exist in the html but where caught in the BeforeNavigate2 event handler and runs some other abitary code which is the requirement.
I thought similar could be done by using (Language C#):
webView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
webView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
But the event CoreWebView2_WebResourceRequested doesn't event get fired. It gets fired when there is a valid hyperlink or leaving out the #.
Their is no javascript or id's in these HTML files. These HTML files are already deployed to end users and are not easily replaced.
How do I resolve this. I'm currently using build 1.0.1072.54 or Microsoft.Web.WebVew2.
Hey @NeilHayes - Could you try using the NavigationStarting event and see if that works here? Let me know if that doesn't work. Thanks!
No the browser naviagtion event doesn't fire either. In my case I found the actual Anchor Tags where in an iFrame, so currently the only way I have found to get it to work is to add Javascript code to all the html files. From the files in the iFrame post a message to the parent, the parent catch the message and post that message on.....trouble is I don't own the html files.
There's also a CoreWebView2.FrameNavigationStarting event. Does that work?
Here are 3 basic files
- Call it menu.htm
<html>
<head>
<meta http-equiv=Content-Type content='text/html;'>
<title>Options</title>
</head>
<frameset cols='216,1*'>
<frame name=LeftFrame src=TOCFrame.htm>
<frame name=MainFrame src='Test1.htm'>
</frameset>
</html>
- Call it TOCFrame.htm
<html><head><base target=MainFrame>
<title>TOC</title>
</style>
</head>
<body>
<div >
<h3>Main Options</h3>
<a href=Test1.htm>
Put out the fire
</div></body></html>
- Call it Test1.htm
<html xmlns:v='urn:schemas-microsoft-com:vml'>
<head>
</head>
<body>
<h1><a name='_top'></a></h1>
<a href='#WATER'>Water</a>
</body>
- All I need to trap is #WATER was clicked on
The CoreWebView2.FrameNavigationStarting event does not trap that. Reminder : I don't own the html files, I can't change them.
I've confirmed that NavigationStarting never fires for anchor tags. I'm opening this as a bug on our backlog. Thanks!
Is there a workaround for intercepting anchor tags
We couldn't wait for the bug fix, so we redesigned.
might have to switch to CEF now