The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
[UWP][C#] Script Notify Webview not working RRS feed

  • Question

  • Hi! I am trying to follow one article on msdn blog. I have added the script notify event as mentioned but the textbox is not being updated. The method itself is not being called. Please help thanks.

    WebView xaml:

    <WebView x:Name="MyWebview" Height="500" Source="http://localhost/webview.php" Margin="0,0,10,0"

    ScriptNotify="MyWebview_ScriptNotify_1"/>

    C# code:

    private void MyWebview_ScriptNotify_1(object sender, NotifyEventArgs e)
            {
                MyTextBox.Text = e.Value;
            }

    Website javascript function:

    function TimeUpdate() {
     var TimeTextbox = document.getElementById("TheTime");
     TimeTextbox.value = new Date().toTimeString();
     window.external.notify(TimeTextbox.value);
     }

    Suggestions will be appreciated. Thanks!

    .

    Thursday, September 17, 2015 5:35 PM

Answers

  • Hi Venkata,

    I see that you are using window.external.notify, based on this article, we can see the following information: 

    To enable an external web page to fire the ScriptNotify event when calling window.external.notify, you must include the page's URI in the ApplicationContentUriRules section of the app manifest. (You can do this in Visual Studio on the Content URIs tab of the Package.appxmanifest designer.) The URIs in this list must use HTTPS and may contain subdomain wildcards (for example, "https://*.microsoft.com"), but they can't contain domain wildcards (for example, "https://*.com" and "https://*.*"). The manifest requirement does not apply to content that originates from the app package, uses an ms-local-stream:// URI, or is loaded using NavigateToString.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 7, 2015 4:27 AM
    Owner

All replies

  • Hi Venkata,

    Welcome to the Developing Universal Windows apps forum!

    I have tested your code in my side, since I do not have your website code http://localhost/webview.php, so I have created the following html code:

    <html>
    <head>
    <script type="text/javascript">
     function TimeUpdate() {
     var TimeTextbox = document.getElementById("TheTime");
     TimeTextbox.value = new Date().toTimeString();
     window.external.notify(TimeTextbox.value);
      }
    </script>
    </head>
    <body style="background-color:aqua">
       <h2>This is a WebView</h2>
        <input type="text" id="TheTime" style="width:300px"/>
        <button type="Button" onclick="TimeUpdate()">click me</button>
    </body>
    </html>

    After that your code works very well in my side with the following result:

    So first please try to check your website code to make sure that it works very well in the browser. Then please try to add a breakpoint or try-catch to see what happen.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.





    Friday, September 18, 2015 6:45 AM
    Owner
  • Hi Amy, 

    Thanks for your timely response. I have tried using your html code as well, it is working well on the browser. Even then my textbox value is not getting updated. The InvokeScript method works fine but ScriptNotify doesnt work. I am attaching the C# code for your reference.

    This method is working:

    private async void MyButton_Click_1(object sender, RoutedEventArgs e) { await MyWebview.InvokeScriptAsync("TimeUpdate", null); }

    This method is not working:

    private void MyWebview_ScriptNotify_1(object sender, NotifyEventArgs e)
            {
                 MyTextBox.Text = e.Value;
            }

    Should I add a list of urls allowed? I have read somewhere that only https urls can allow script notify to execute.  Please suggest any possible solution. Thanks!



    Friday, September 18, 2015 7:26 AM
  • Hi Venkata,

    I see that you are using window.external.notify, based on this article, we can see the following information: 

    To enable an external web page to fire the ScriptNotify event when calling window.external.notify, you must include the page's URI in the ApplicationContentUriRules section of the app manifest. (You can do this in Visual Studio on the Content URIs tab of the Package.appxmanifest designer.) The URIs in this list must use HTTPS and may contain subdomain wildcards (for example, "https://*.microsoft.com"), but they can't contain domain wildcards (for example, "https://*.com" and "https://*.*"). The manifest requirement does not apply to content that originates from the app package, uses an ms-local-stream:// URI, or is loaded using NavigateToString.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 7, 2015 4:27 AM
    Owner
  •  you must include the page's URI in the ApplicationContentUriRules

    What about the case when you do not know the URI at run time?

    When the URI is a dynamic value.

    thanks

    Cole

    Thursday, March 2, 2017 9:01 PM
  • The manifest requirement does not apply to content that originates from the app package, uses an ms-local-stream:// URI, or is loaded using NavigateToString

    ScriptNotify is not working in UWP apps without adding ms-appx-web or URI. 

    When the URI is combined with package name, ScriptNotify for webview does not work either is there anything wrong that we are doing, 

    Tuesday, June 27, 2017 5:14 PM
  • Thank you so much,  that exactly get my problem solved.

    Friday, November 10, 2017 4:32 AM