How to pass inputs to javascript functions with cefsharp

谁都会走 提交于 2019-12-01 08:17:21

I figured out how to do what I wanted... and it was relatively straightforward.

If you want the functionality of the WebBrowser's InvokeScript function

this.webBrowser.InvokeScript("functionName", input0, input1, input2);

with the CefSharp WebView then you just have to do something like this:

webView.ExecuteScript(String.Format("functionName({0},{1},{2});", input0, input1, input2));

You will have to make sure to escape any string parameters correctly since you are calling a javascript function where the inputs will be filled in with your values and therefore will be treated as string literals.

If you really don't want to change your code and you are swapping out the WebBrowser with a WebView then you could make an extension method that adds "InvokeScript" to the WebView.

I tried out above function, didn't work. Finally figured it out, missing quotes for each placeholders.

Something like this:

webView.ExecuteScript(String.Format("functionName('{0}','{1}','{2}');", input0, input1, input2));
user3934404

You can execute JavaScrip from webView

webView.ExecuteScript("document.getElementById...");
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!