How to emulate Navigating event in the WebView.


While working on the Win8 version of the FeedPoint app, I’ve encountered the situation when I needed to be able to intercept a navigation in the WebView control via the link click from the user and show a full browser window instead. Unfortunately the WebView control in the WinRT currently does not implement Navigating event.

The workaround is based on the functionality in the WebView control which allows raising ScriptNotify event via the javascript code on the page. This javasript code needs to have a call: window.external.notify(a) and the ScriptNotify event will be raised in your managed app. So I added the code to parse the html for all links (<a>) and make it look like that:

<a href=”blabla.htm” onclick=”return OnLinkClick(‘blabla.htm’)>Blabla Link</a>

Where the OnLinkClick is a simple javascript function:

 

 <script type=’text/javascript’>”);
  function OnLinkClick(a) {
       window.external.notify(a);
       return false;
    }
</script>

You should notice that this function returns false. This is done in order to cancel default navigation of the WebView control. And here’s how ScriptNotify event handler looked like in my app:

 private async void webViewContent_ScriptNotify(object sender, NotifyEventArgs e)
 {
      // Launch external browser
      await Windows.System.Launcher.LaunchUriAsync(new Uri(e.Value));
 }

Comments (3)

  1. Hello!

    Good solution, thanks.

    How did you make injecting this javascript into pages?

    Thanks.

  2. gdfgklj says:

    Useless. Only works if you control the web page.

  3. GVM says:

    ScriptNotify doesnt work when script is running on HTTPS … how to get notifications from HTML pages coming from HTTPS? What works on windows 8 doesnt work on windows 8.1! MS is total crap.