在webview WinRt中获取导航URL

时间:2012-12-10 12:24:10

标签: javascript javascript-events windows-8 webview microsoft-metro

我正在注入此JS以在运行时获取navigating urldestination)。我正在获得JS异常。有什么问题?

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

另外请告诉我如何cancel the navigation并使用此JS返回上一页?

1 个答案:

答案 0 :(得分:1)

如果我理解正确,因为我还没有使用过WebView(你),你想在离开之前报告当前位置然后取消它吗?

你可以试试这个:

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

我不确定你为什么在那里有引号; location.href将在该Web视图中,它将作为字符串传递。

另外,请查看对this thread的回复。其中一人说:

  

你的问题解决了吗?在Release Preview中,您需要   添加一些看起来像这样的代码:

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

另一件事,这个帖子:Can I get the current HTML or Uri for the Metro WebView control?,讨论了LoadCompleted事件处理程序。

看看是否有帮助。