2

Ok, this is silly. I have an existing variable in the HTML I load into my WebView, called caret_position

I want to retrieve it via InvokeScriptAsync in my Windows Store app. Here's what I've tried.

return await HtmlEditor.InvokeScriptAsync("eval", 
    new string[] { "return caret_position;" });

return await HtmlEditor.InvokeScriptAsync("eval", 
    new string[] { "caret_position;" });

Neither of these work. The only way I can get it to work is to write it to a hidden input in javascript and then use this call.

return await HtmlEditor.InvokeScriptAsync("eval", 
    new string[] { "document.getElementById('this_is_silly').value;" });

Is there a way I can just access the javascript variable directly or do I have to jump through these hoops every time?

roryok
  • 9,325
  • 17
  • 71
  • 138

1 Answers1

6

InvokeScriptAsync works only with string return values; if caret_position is a number, then it will cause trouble in the conversion. That is, a function {return caret_position; } would be returning a number when eval'd. So you need to make sure that function is returning a string, e.g. return caret_position.toString();

  • I'll try that out and report back – roryok Aug 05 '14 at 09:46
  • This is the correct answer. I assumed InvokeScriptAsync() would return the result AS a string, but apparently it returns it only if it IS a string. Make sure to call .toString() in your JS function if you want to use the return value in c# – buddybubble Aug 20 '14 at 10:51
  • 1
    Great answer. You made my day. Simple use `'' + caret_position` and you'll get the result. Why it can't convert number to string? stupid – Mike Keskinov Mar 29 '17 at 16:53