adheus adheus - 1 year ago 110
C# Question

How can I disallow WebView to open links on the browser in WinRT( target=_blank links )?

I have a WebView on my app and I can't change the html file("target=_blank" link types). But some links on the page makes my app open them on the system browser. How can I disallow this action?


Answer Source

In the NavigationCompleted event handler run this script:

webView.InvokeScriptAsync("eval", new[]
                    var hyperlinks = document.getElementsByTagName('a');
                    for(var i = 0; i < hyperlinks.length; i++)
                        if(hyperlinks[i].getAttribute('target') != null)
                            hyperlinks[i].setAttribute('target', '_self');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download