javascriptwebviewwindows-8microsoft-metro

Get the navigating url in webview WinRt


I am injecting this JS to get the navigating url (destination) at run time. I am getting JS exceptions. What is wrong?

     static String NAVIGATING_FUNCTION = "window.onbeforeunload = function(){ window.external.notify(' + location.href + ''); };";
     webView.InvokeScript("eval", new String[] { NAVIGATING_FUNCTION });

Also please tell me how to cancel the navigation and return back to the previous page using this JS?


Solution

  • If I understand correctly, since I haven't used the WebView (yet), you want to report the current location before leaving and then cancel it?

    You could try this:

    window.onbeforeunload = function(){ window.external.notify(location.href); return false; };
    

    I am not sure why you had quotes in there; location.href would be in that web view, and it would be passed as a string.

    Also, take a look at the responses to this thread. One of them says:

    Did you get your problem solved? In the Release Preview, you need to add some code that looks like this:

            List<Uri> allowedUris = new List<Uri>();
            allowedUris.Add(e.Uri);
            allowedUris.Add(new Uri("http://www.bing.com"));
            Browser.AllowedScriptNotifyUris = allowedUris;
    

    Another thing, this thread: Can I get the current HTML or Uri for the Metro WebView control?, talks about the LoadCompleted event handler.

    See if that helps.