0

I am developing Windows Store Apps with C#/xaml.

How do I notice if a link in my WebView gets klicked? There is no Event for that afaik. I've already looked into this and that but it doesn't provide me with a suitable solution.

May I give an example: I am embedding the stream of a facebook page in my WebView via iFrame. I want to block all links that might be in that iFrame.

Community
  • 1
  • 1
Johann
  • 523
  • 9
  • 22

1 Answers1

1

You can call InvokeScript with some of your own Javascript to set up a listener for when the user navigates away from your page. This would look something like the following in C#:

var navigationListenerString = @"
(function() {
  function leavingPage() {
    window.external.notify("LEAVING PAGE");
  }
  window.onbeforeunload = leavingPage;
})()";

webView.InvokeScript("eval", new string[] { navigationListenerString });

Then you can use ScriptNotify to listen for your particular message to determine that the page is unloading and the user is leaving. Unfortunately you cannot detect where a user is going. Also, if the hyperlink opens in a new window and the webview does not unload, you cannot detect that either.

fecub
  • 945
  • 10
  • 27
  • I get following SystemException while trying this: DISP_E_UNKNOWNNAME – Johann Mar 27 '13 at 15:30
  • After some fiddeling around I found the solution: You've probably done it too soon. You should do it only after loading the page. Make sure you use it in TestWebView_LoadCompleted(...) or later. Also note that the webpage could send the LEAVING PAGE aswell, so maybe generate a random key or something. Plus, note that you will need to add the webpage (or all webpages using AnyScriptNotifyUri) to allowedscriptnotifyuris list. http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.webview.allowedscriptnotifyuris.aspx – domenukk May 01 '13 at 01:18