WebView.InvokeScriptAsync method

February 8, 2015 · Dev App

Just because I banged my head against my desk trying to solve that one for a while tonight. I should have read the documentation a little bit more carefully in the first place. However, if this post can help someone...

When using a WebView component to display HTML content in a WindowsPhone App, you might have the (silly) idea to interact via Javascript with your DOM via the C# WebView.InvokeScriptAsync() function. Beware:

The invoked script can return only string values.

You read that right. If you - for instance - needed to find the window.body.clientHeight of a page - which returns a number - you will keep on getting a silent exception and an empty string as a return value until you finally understand that you need a .toString() in there.

Here the full call... just for fun:

var clientHeight = await this.WebView.InvokeScriptAsync("eval", new string[] { "document.body.clientHeight.toString();" });

You're welcome.

Comments powered by Disqus