locked
open Webview links in external Browser if target is set to _blank RRS feed

  • Question

  • User155845 posted

    Hi,

    i know how to open links in the external browser, but i want to do it only if the target of the link is set to _blank. Any idea how to do that?

    Thanks

    Helge

    Monday, August 21, 2017 12:46 PM

Answers

  • User155845 posted

    if someone needs it, i got it

            private void MyWebView_Navigated(object sender, WebNavigatedEventArgs e)
            {
                            string jscall = "var d = document.getElementsByTagName('a'); for (var i = 0; i < d.length; i++) { if (d[i].getAttribute('target') == '_blank') { if(d[i].href.includes('?')) {d[i].href = d[i].href + '&target=blank';} else {d[i].href = d[i].href + '?target=blank';}}}";
                            myWebView.Eval(jscall);
            }
            private void MyWebView_Navigating(object sender, WebNavigatingEventArgs e)
            {
                if (e.Url.ToLower().Contains("target=blank"))
                {
                    var uri = new Uri(e.Url);
                    Device.OpenUri(uri);
                    e.Cancel = true;
                }
                else
                {
                    e.Cancel = false;
                }
            }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, August 21, 2017 4:02 PM

All replies

  • User155845 posted

    if someone needs it, i got it

            private void MyWebView_Navigated(object sender, WebNavigatedEventArgs e)
            {
                            string jscall = "var d = document.getElementsByTagName('a'); for (var i = 0; i < d.length; i++) { if (d[i].getAttribute('target') == '_blank') { if(d[i].href.includes('?')) {d[i].href = d[i].href + '&target=blank';} else {d[i].href = d[i].href + '?target=blank';}}}";
                            myWebView.Eval(jscall);
            }
            private void MyWebView_Navigating(object sender, WebNavigatingEventArgs e)
            {
                if (e.Url.ToLower().Contains("target=blank"))
                {
                    var uri = new Uri(e.Url);
                    Device.OpenUri(uri);
                    e.Cancel = true;
                }
                else
                {
                    e.Cancel = false;
                }
            }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, August 21, 2017 4:02 PM
  • User244278 posted

    Thanks @HelgeKeck worked great.

    Monday, November 11, 2019 9:36 PM