locked
WebView external web page RRS feed

  • Question

  • I have a webView with an external html page loaded in it.

    Is it possible to somehow control any event inside that webView so that i can open an external browser when the user click one of buttons?

    The application is written in C# and XAML

    Thursday, June 5, 2014 1:04 PM

Answers

  • Have you tried with WebView.ScriptNotify event?

    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, June 5, 2014 1:11 PM
  • Hi,

    You can follow up these steps:

    1.Add the following Javascript code to your HTML:

    "<script type=\"text/javascript\">for (var i = 0; i < document.links.length; i++) { document.links[i].onclick = function(){window.external.notify(\"LaunchLink:\" + this.href); return false; } }</script>"

    This adds an onclick handler to every link (<a href="..."></a>) on the page. window.external.notify is a Javascript method that works in the Webview.

    2.Add the ScriptNotify event handler to the webview:

    MyWebview.ScriptNotify += WebView_ScriptNotify;

    3.Declare the event handler:

    async private void WebView_ScriptNotify(object sender, NotifyEventArgs e)
            {
                try
                {
                    string data = e.Value;
                    if (data.ToLower().StartsWith("launchlink:"))
                    {
                        await Launcher.LaunchUriAsync(new Uri(data.Substring("launchlink:".Length), UriKind.Absolute));
                    }
                }
                catch (Exception)
                {
                    // Could not build a proper Uri. Abandon.
                }
            }

    And you can refer to the links to see remarks to allowed Uris whitelist:

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

    Remarks

    A hosted HTML page can fire the ScriptNotify event in your Windows Store app when the page calls window.external.notify and passes a string parameter.

    Note  Because this event is initiated by external code, you should be careful about what you put in the event handler. To prevent malicious scripts from exploiting this event, be sure to enable it only for trusted URIs, as described below.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, June 6, 2014 5:31 AM

All replies

  • Have you tried with WebView.ScriptNotify event?

    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, June 5, 2014 1:11 PM
  • The event is not firing...I guess I would have to get some access to the html page to use some script there
    Thursday, June 5, 2014 1:19 PM
  • Hope you went through the REMARKS section in the documentation. 

    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, June 5, 2014 2:01 PM
  • Hi,

    You can follow up these steps:

    1.Add the following Javascript code to your HTML:

    "<script type=\"text/javascript\">for (var i = 0; i < document.links.length; i++) { document.links[i].onclick = function(){window.external.notify(\"LaunchLink:\" + this.href); return false; } }</script>"

    This adds an onclick handler to every link (<a href="..."></a>) on the page. window.external.notify is a Javascript method that works in the Webview.

    2.Add the ScriptNotify event handler to the webview:

    MyWebview.ScriptNotify += WebView_ScriptNotify;

    3.Declare the event handler:

    async private void WebView_ScriptNotify(object sender, NotifyEventArgs e)
            {
                try
                {
                    string data = e.Value;
                    if (data.ToLower().StartsWith("launchlink:"))
                    {
                        await Launcher.LaunchUriAsync(new Uri(data.Substring("launchlink:".Length), UriKind.Absolute));
                    }
                }
                catch (Exception)
                {
                    // Could not build a proper Uri. Abandon.
                }
            }

    And you can refer to the links to see remarks to allowed Uris whitelist:

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

    Remarks

    A hosted HTML page can fire the ScriptNotify event in your Windows Store app when the page calls window.external.notify and passes a string parameter.

    Note  Because this event is initiated by external code, you should be careful about what you put in the event handler. To prevent malicious scripts from exploiting this event, be sure to enable it only for trusted URIs, as described below.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, June 6, 2014 5:31 AM
  • I cannot intercept the click events. Are there any conditions for this to happen?

    I followed all the steps but it does not steps through my event handler.

    Thanks

    • Edited by adrianadrianp Friday, June 6, 2014 1:40 PM wrong type
    Friday, June 6, 2014 1:39 PM
  • Working... thanks! Script Notify is the solution
    Friday, June 6, 2014 3:52 PM