WebView2 - javascript link

myNameRon

Member
Joined
Jan 9, 2021
Messages
9
Programming Experience
Beginner
Hi,
I'm trying to switch from using webBrowser to webView2 control, in my WinForm application. With webBrowser, I'm able to capture the URL of a link (when clicked) on the webpage., even if it's javascript (javascript:_doPostBack('btnCancel','')) via the webBrowser1_Navigating event (e.URL). I'm not able to find a way to do this via webView2. Note: I've tried "webView21.Source.OriginalString", but not getting any detection that the link was clicked.

thanks,
Ron
 
How about e.Uri in NavigationStarting event?
 
Are you talking about a detecting and cancel a new window opening? .CoreWebView2.NewWindowRequested event allows that, it has e.Uri too.
 
Are you talking about a detecting and cancel a new window opening? .CoreWebView2.NewWindowRequested event allows that, it has e.Uri too.

No new window, the link (javascipt) just affects the current webpage (note: the webpage is not in my control). Maybe webView2 just doesn't detect a javascript command when a link/button is clicked. I can live with this, but just trying to recreate the same functions I implemented with webBrowser control.
 
Do you have a sample webpage I can test where WebBrowser control raise navigation event on element click and WebView2 don't?
 
Do you have a sample webpage I can test where WebBrowser control raise navigation event on element click and WebView2 don't?
HTML:
<html>
<body>
<h1>Test Page</h1>
 <a id="btnCancel" class="buttonCancel" href="javascript:__doPostBack('btnCancel','')">Cancel - Javascript</a>
    <br />
 <a id="btnCancel2" class="buttonCancel" href="http://www.google.com">Cancel - URL</a>
</body>
</html>

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    // Let me see...
    MessageBox.Show(e.Url.ToString());
   
    if (e.Url.ToString().Contains("btnCancel"))
    {
        this.Close();
    }
}
 
Last edited by a moderator:
That's not testable from my end unfortunately.
 
I was more thinking of an url I can point the browsers to for testing out behaviours and possible solutions.
 
I was finally able to set up some tests. I can't find any event that detects this case with WebView2 at the moment, but it is possible to inject a javascript to add click event handler and post back to WebMessageReceived event for example like this:
C#:
private void webView21_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
{
    var script = @"document.addEventListener('click', function (e) {
        if (e.target instanceof HTMLAnchorElement && e.target.href.startsWith('javascript')) {
            window.chrome.webview.postMessage(JSON.stringify(e.target.href));
        }
    });";
    _ = webView21.ExecuteScriptAsync(script);
}

private void webView21_WebMessageReceived(object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs e)
{
    Debug.WriteLine(e.TryGetWebMessageAsString());
}
 
I was finally able to set up some tests. I can't find any event that detects this case with WebView2 at the moment, but it is possible to inject a javascript to add click event handler and post back to WebMessageReceived event for example like this:
C#:
private void webView21_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
{
    var script = @"document.addEventListener('click', function (e) {
        if (e.target instanceof HTMLAnchorElement && e.target.href.startsWith('javascript')) {
            window.chrome.webview.postMessage(JSON.stringify(e.target.href));
        }
    });";
    _ = webView21.ExecuteScriptAsync(script);
}

private void webView21_WebMessageReceived(object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs e)
{
    Debug.WriteLine(e.TryGetWebMessageAsString());
}

Thanks John! I feel like I'm taking up too much of you time on this. It's not that critical to resolve, as the client can just click the WinForm's X (close) button. I'm new to webView2, and thought I might be missing something stupid (as I typically am). Also (is it just me?), I could not find a lot of "real world" code examples of using webView2 on the internet. Thanks again.
 
Back
Top Bottom