Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

Thanks.

like image 398
adheus Avatar asked Jul 02 '13 18:07

adheus


3 Answers

In the NavigationCompleted event handler run this script:

webView.InvokeScriptAsync("eval", new[]
            {
                @"(function()
                {
                    var hyperlinks = document.getElementsByTagName('a');
                    for(var i = 0; i < hyperlinks.length; i++)
                    {
                        if(hyperlinks[i].getAttribute('target') != null)
                        {
                            hyperlinks[i].setAttribute('target', '_self');
                        }
                    }
                })()"
            });
like image 84
Aliaksandr Hmyrak Avatar answered Sep 27 '22 15:09

Aliaksandr Hmyrak


On Windows 10, you can use WebView.NewWindowRequested:

private void WebView1_NewWindowRequested(
    WebView sender,
    WebViewNewWindowRequestedEventArgs args)
{
    Debug.WriteLine(args.Uri);
    args.Handled = true; // Prevent the browser from being launched.
}
like image 42
kiewic Avatar answered Sep 27 '22 16:09

kiewic


There is a navigation starting event. It have a cancel property that can be used to cancel the navigation. Maybe this will work for you?

http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.webview.navigationstarting

like image 38
user1985513 Avatar answered Sep 27 '22 16:09

user1985513