【问题标题】:Get the navigating url in webview WinRt获取 webview WinRt 中的导航 url
【发布时间】:2012-11-27 20:43:08
【问题描述】:

我正在注入此 JS 以在运行时获取 navigating url (destination)。我收到 JS 异常。怎么了?

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

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

【问题讨论】:

  • 你在函数末尾的引用搞砸了;尝试粘贴到编辑器中进行更正。此外,如果您想取消导航,请在 onbeforeunload 处理程序的末尾尝试 return false

标签: javascript webview windows-8 microsoft-metro


【解决方案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 事件处理程序。

看看有没有帮助。

【讨论】:

  • 而不是当前位置,我希望在导航之前知道目的地并取消导航。这可能吗?
  • 并非如此。 “不幸的是,似乎没有办法提取目标 url,也无法取消导航,这与 WebBrowser 控件具有的真正导航事件不同”——来自nicksnettravels.builttoroam.com/post/2012/04/21/…
猜你喜欢
  • 1970-01-01
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多