window.external.notify in WebView


  • Can we use window.external.notify in WebView?

    When I was trying to use window.external.notify, an exception is thrown in Javascript. If not, how can I notify an event to native code (C++) using JS in WebView?

    Kapil Goel
    Monday, October 3, 2011 4:11 PM

All replies

  • Hi Kapil,

    JavaScript and C++ can communicate using the Windows Runtime.  Can you share usage scenario in more detail and I can recommend the right thing to do.


    Raman Sharma, Visual C++

    Monday, October 3, 2011 7:35 PM
  • Raman,

    Thanks for the reply.

    I am writing an application in which if user selects/highlights a word/words on a webpage, I want to show some native controls on top of a WebView. I have a javascript code which fires an event whenever user selects/highlights a word/words. Now I want to pass this event to native code.

    Previously, when we were using the WebBrowser, you can fire an event from javascript using window.external.notify. And we have added an event listener,  for NotifyScriptEvent on WebBrowser in C++. Ithought same would be true with WebView. As WebView also has NotifyScriptEvent.

    So, I was wondering how can I fire an event from javascript to WebView control.

    Kapil Goel
    Monday, October 3, 2011 7:58 PM
  • Kapil: Just so I understand your question better, are you trying to build this application using XAML/C++ or using HTML/JS?

    The answer in both cases would be different. 

    If you are building using HTML/JS, then you dont need any controls in order to get a "web view".  The default rendering is done in HTML.  And in the default JavaScript file can talk to WinRT components.

    Monday, October 3, 2011 9:48 PM
  • Raman: It is a XAML/C++ application in which I am hosting a WebView.
    Kapil Goel
    Monday, October 3, 2011 10:03 PM
  • Yes you can use window.external.notify from JavaScript within the HTML page hosted in WebView and pass a notification string. This will raise the ScriptNotify event on the WebView control. Note the NavigationDomain property which allows the app to optionally control the domain from which scripts can call ScriptNotify.

    Can you doublecheck your approach based on the above and see if the exception still gets thrown? If so, knowing more details on the exception would help us debug what's going on.

    Ashish Shetty, Program Manager, Microsoft | http://nerddawg.blogspot.com
    Tuesday, October 4, 2011 4:09 PM
  • Ashish,

    When our application starts, it navigates to http://www.bing.com in the WebView. For testing, when we receive LoadCompleted event on WebView, we inject a javascript on the page using WebView->InvokeScript. This javascript code is pretty simple, first it changes the background color of the page, and then tries to fire an event to native code using window.external.notify.


    try {

        document.body.style.backgroundColor = '#FF0000';

        window.external.notify('Color changed');

    } catch (err) {




    Through debugging, we found that 'notify' is not a property/function of window.external. Means window.external.notify returns 'null'. I don't know if I have to set any property on WebView to enable window.external.notify in the WebView.

    Kapil Goel
    Tuesday, October 4, 2011 4:37 PM
  • Kapil,

    I tried this and it worked fine. Did you inject the script in the LoadCompleted event handler using InvokeScript and eval? Try a simple repro C# app and see if that works. Let me know if you are still stuck.

    - Vinoo Cherian, Windows Development, Microsoft


    Friday, October 21, 2011 4:08 PM
  • I'm having a similar issue.  I made a simple c# app:


    public MainPage()
               // MainWebview.NavigationDomain = new Uri("http://www.bing.com");
                MainWebview.LoadCompleted += new Windows.UI.Xaml.Navigation.LoadCompletedEventHandler(MainWebview_LoadCompleted);
                MainWebview.ScriptNotify += new NotifyEventHandler(MainWebview_ScriptNotify);
                MainWebview.Navigate(new Uri("http://www.bing.com"));
            void MainWebview_LoadCompleted(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
                MainWebview.InvokeScript("eval", new string[] { "document.body.style.background='#1CFE00';window.external.notify('test')" });
            void MainWebview_ScriptNotify(object sender, NotifyEventArgs e)
    And I get an exception on the window.external.notify statement, with or without the navigation domain set.


    • Edited by Ben McMahan Tuesday, January 17, 2012 11:07 PM
    Tuesday, January 17, 2012 11:05 PM
  • I have the same problem as Kapil, I use C# and test the example http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.scriptnotify(v=vs.95).aspx, but it still not work, I need more help, thank you!
    Thursday, April 19, 2012 1:55 PM
  • I have a same as you,  have you solved the problem?
    Thursday, April 19, 2012 2:13 PM
  • 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(new Uri("http://www.bing.com"));
                Browser.AllowedScriptNotifyUris = allowedUris;

    Sunday, July 22, 2012 2:39 PM