locked
InvokeScriptAsync() fails to call

    Question

  • Hi,

    I'm using a webview and while the content renders we have our associated JS files loaded.

    We receive external notifications from JavaSCript to native app code but on the other hand, we are unable to invoke a script that is contained outside.

    Please see below example


    String ^ htmlFragment = L"<!DOCTYPE html>\n" +
    "<html>\n" +
    "  <head>\n" +
    "    <script type = 'text/javascript'>\n" +

    " function onResponse(){ \n" +
    //"var response = JSON.parse(jsonResponse); \n" +
    //" var callback = response.context; \n" +
    "document.getElementById('myDiv').innerText = 'called form onresponse'; \n" +
    "return 'hey you'; \n"
    "      }\n" +

    "      doSomething: function(str) {\n" +
    "        document.getElementById('myDiv').innerText = str;\n" +
    "        return 'Hello World!';\n" +
    "      }\n" +

    "    </script>\n" +
    "  </head>\n" +
    "  <body>\n" +
    "    <div id = 'myDiv'>Hello</div>\n" +
    "  </body>\n" +
    "</html>\n";

    There is an anonymous function defined in the above script. The property doSomething's value will be set by this function call.

    Now, when i try to invoke this method "onResponse", it throws an exception stating "unknown name"


    create_task(webView5->InvokeScriptAsync(L"onResponse", nullptr)).then([this](String ^result)
    {
    Platform::Details::Console::WriteLine(L"Result from script " + result);
    });

    If i remove this chunk (anonymous function) from the above html/JS code, it works ( i mean OnResponse method is called).

    So, why is InvokeScriptAsync() crashing when we have properties and their values defined in the form of anonymous function names ?

    How do we invoke scripts in such a case because we have various properties and their values set through function return values in our case ?

    Can someone please immediately help on this or throw some light on how this API works ?

    Thanks !!

    Monday, May 12, 2014 11:14 AM

All replies

  • At what time is the HTML/JS being loaded into the webview?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, May 12, 2014 11:04 PM
    Moderator
  • Webview is loaded with the above HTML on a load HTML click.

    // This is the click handler for the 'Load' button.
    void Scenario5::load_Click(Object^ sender, RoutedEventArgs^ e)
    {
    // Grab the HTML from the text box and load it into the WebView
    webView5->NavigateToString(HTML3->Text);
    }

    //This is the click handler for the 'Script' button.
    void Scenario5::script_Click(Object^ sender, RoutedEventArgs^ e)
    {

    // Invoke the javascript function called 'SayGoodbye' that is loaded into the WebView.
    //create_task(webView5->InvokeScriptAsync(L"Client.doSomething", nullptr)).then([this](String ^result)
    create_task(webView5->InvokeScriptAsync(L"onResponse", nullptr)).then([this](String ^result)
    {
    Platform::Details::Console::WriteLine(L"Result from script " + result); //this works but InvokeScriptAsync(L"doSomething" or L"client.doSomething",nullptr) won't as  those functions are anonymous
    });
    }

    Tuesday, May 13, 2014 2:07 AM