Kinjan Bhavsar Kinjan Bhavsar - 10 months ago 259
Javascript Question

How to invoke javascript in WebView Windows 10 UWP?

I am trying to load a javascript in WebView to do some calculations and get the output in a string. I tried to use following code

string htmlFragment = "<html><head><script type='text/javascript'>" +
"function doubleIt(incoming){ " +
" var intIncoming = parseInt(incoming, 10);" +
" var doubled = intIncoming * 2;" +
" doubled.toString() + 'px';" +
" return doubled.toString());" +
"};" +
"</script></head><body>" +
"<div id = 'myDiv'>I AM CONTENT</div></body></html>";
htmlView.LoadCompleted += async(s1,e1) =>
string result = await htmlView.InvokeScriptAsync("eval", new string[] { "doubleIt(25)" });

I am able to load simple javascript easily now based on help provided in the answer. But now I am facing issues when there is more than one function in javascript, I am getting an exception. I am trying the following code

string htmlFragment = @"<html><head><script type='text/javascript'>" +
"function a(){return 10;};" +
"function b(){return 20;};" +
"function c(){return 30;};" +
"return (a()*b()*c());" +
"</script></head><body>" +
"<div id = 'myDiv'>I AM CONTENT</div></body></html>";

Please suggest.

Answer Source

The documentation for this feature is really poor. It took me some time to figure out how to invoke Javascript in UWP WebView

When you first look at the function call webView.InvokeScriptAsync(string,string[]) your initial reaction is that they want the function name as the first parameter and then the function paramaeters as the string array. (mainly because the MSDN documentation says this)



Type: System.String [.NET] | Platform::String [C++]

The name of the script function to invoke.


Type: System.String[] [.NET] | Platform::Array [C++]

A string array that packages arguments to the script function.

HOWEVER, this is wrong and will lead to hours of head banging. REALLY, what they want is the word "eval" in the first parameter and then a string array of functions, and or commands you wish to eval

   var value = await webViewer.InvokeScriptAsync("eval", 
      new string[] 

Having worked with Microsoft APIs for a few years now I am convinced that this is not the intended way of consuming this function and is a bit of a hack. Unfortunately if you want to consume JavaScript this is the only way that I know that works currently.