adheus
adheus

Reputation: 4045

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.

Upvotes: 9

Views: 6505

Answers (6)

Andrei Ashikhmin
Andrei Ashikhmin

Reputation: 2451

Stumbled on this myself recently, and I want to add that even though user2269867's answer is a viable solution, it might not work in certain situations.

For example, system browser will not only open if user click a link with target="_blank" attribute, but also if window.open() function called in javascript. Moreover, even removing all 'target' attributes won't work if a page loading some content dynamically and changing DOM after your script is already finished executing.

To solve all problems above, you need to override window.open function and also check for 'target' attribute not once, but every time user click something. Here is script that covers those cases:

function selfOrParentHasAttribute(e, attributeName) {
    var el = e.srcElement || e.target;

    if (el.hasAttribute(attributeName)) {
        return el;
    }
    else {
        while (el = el.parentNode) {
            if (el.hasAttribute(attributeName)) {
                return el;
            }
        }
    }

    return false;
}

var targetAttributeName = "target";

document.addEventListener("click", function (e) {
    var el = selfOrParentHasAttribute(e, targetAttributeName);

    if (el) {
        if ((el.getAttribute(targetAttributeName) == "_blank") ||
            (el.getAttribute(targetAttributeName) == "_new"))
        {
             el.removeAttribute(targetAttributeName);
        }
}
});


window.open = function () {
    return function (url) {
        window.location.href = url;
    };
}(window.open);

My js skills aren't ideal, so feel free to modify. Also don't forget that, as kiewic mentioned, for Windows 10 there is WebView.NewWindowRequested event which solves this issue more natural.

Upvotes: 1

kiewic
kiewic

Reputation: 16430

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.
}

Upvotes: 10

Aliaksandr Hmyrak
Aliaksandr Hmyrak

Reputation: 314

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');
                        }
                    }
                })()"
            });

Upvotes: 10

yash
yash

Reputation: 197

If you can edit HTML of the page and NavigateToString(), then add <base target='_blank'/> in the <head>

Upvotes: 0

Jake Bock
Jake Bock

Reputation: 33

If you just want to show the page and not allow any action to be done on that page I would look into WebViewBrush. The WebViewBrush will basically screenshot the website and the users will not be able to use any links or anything else on that page, it will turn into a read-only page. I believe this is what you are asking for.

More info on WebViewBrush can be found here: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.webviewbrush

Upvotes: 0

user1985513
user1985513

Reputation: 481

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

Upvotes: 1

Related Questions