Welcome Guest Search | Active Topics | Sign In | Register

Return a value from C# and passing object to Javascript Options
PhilipT
Posted: Saturday, February 15, 2014 8:11:26 AM
Rank: Advanced Member
Groups: Member

Joined: 2/15/2014
Posts: 52
Hi,

In .NET WebBrowser control, I am able to return a value from C# function and pass object as parameter to JavaScript. Can I do that in EO.WebBrowser too? I looked at the documentation but couldn't find one example.

It seems that the following features are missing when compared to .NET WebBrowser control:
Cannot return a value from C# function to JS
Cannot pass an object as parameter to JS

Will they be available especially returning value from .NET function to js?

Regards
Philip

eo_support
Posted: Saturday, February 15, 2014 10:24:13 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

Thanks for posting in the forum. To return a value from C# to JS, you would set the event argument object's ReturnValue property:

http://www.essentialobjects.com/doc/6/eo.webbrowser.jsextinvokeargs.returnvalue.aspx

To pass value from C# to JS, you would use a JSFunction object:

http://www.essentialobjects.com/doc/6/eo.webbrowser.jsfunction.invoke_overloads.aspx

You can find more details here:

http://www.essentialobjects.com/doc/6/advanced/js.aspx

We will also post a new build very soon which would add a InvokeFunction method on JSObject. So you will be do something like this:

Code: C#
//Call window.alert("hi");
WebView1.GetDOMWindow().InvokeFunction("alert", "hi");


This is a shortcut for the following code:
Code: C#
//Get the JavaScript window object
JSObject window = WebView.GetDOMWindow();

//Get the alert function on the window object
JSFunction alertFunction = window["alert"] as JSFunction;

//Call the alert function with argument "hi"
alertFunction.Invoke(window, "hi");

This has the same effect as:

Code: C#
WebView1.EvalScript("alert('hi')");


Hope this answers your question. Please feel free to let us know if you still have any questions.

Thanks!
PhilipT
Posted: Saturday, February 15, 2014 11:13:56 AM
Rank: Advanced Member
Groups: Member

Joined: 2/15/2014
Posts: 52
Hi,

The return value from C# to js now works, i can now return a value to javascript. Thanks!

However, i still have problem passing an object as parameter to javascript, i will get a runtime exception whenever i try to pass an object as parameter.

JSObject jbObj = m_WebView.GetDOMWindow();

JSFunction func = (JSFunction)m_WebView.EvalScript(functionName);

func.Invoke(jbObj, parameters);

{"Object 'WindowsApplication1.clsPerson' can not be passed to JavaScript code."}
eo_support
Posted: Saturday, February 15, 2014 11:43:48 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

No. You won't be able to pass complex C# objects into JavaScript in the current version. Only two types of objects can be passed into JavaScript:

1. Simple values that has a direct JavaScript corresponding type, such as string, DateTime, etc;
2. A JSObject that were returned to you from the JavaScript world. For example, a DOM element object, etc;

What you can do is to create a Jason string based on your .NET object, then call EvalScript to turn that Jason string into a JavaScript object. After that you will be able to pass that back to the JavaScript. For example, suppose you have a .NET Person class with FirstName and LastName property, you can do something like this:

Code: C#
//This will return a string like {"FirstName": "Joe", "Last Name": "Smith"}
//The following code use the popular Jason.NET library. You can also use the .NET
//built-in JavaScriptSerializer class, which is available since .NET 3.5
string jasonString = Newtonsoft.Jason.JasonConvert.SerializeObject(person);

//Turn the string object into a JavaScript object
JSObject jsPerson = m_WebView.Eval(jasonString);

//Now you can pass jsPerson to func.Invoke


Supporting passing any .NET object directly into the JavaScript world is not hard to implement but have serious performance implications comparing with the above method. With the above method, all properties values are captured and passed to the .NET side with one call and after the call the .NET object can be discarded. The other method is to hold the .NET object on the .NET side, and create a proxy object on the JavaScript side that points to this .NET object, then whenever you access any property/method of that proxy object from your script, that proxy object calls back to the .NET side to access the corresponding .NET property/calling the .NET method. This can easily drag down the performance because not only the JavaScript <-> .NET crossing is relatively expensive, but also all calls has to be synchronized on the .NET side. This can potentially causing more problems for you in the long run even though it can get you started very quickly. So we are a bit hesitate to implement that.

Thanks
PhilipT
Posted: Saturday, February 15, 2014 11:52:19 AM
Rank: Advanced Member
Groups: Member

Joined: 2/15/2014
Posts: 52
Ok, that makes sense.

Appreciate your quick response.
eo_support
Posted: Saturday, February 15, 2014 9:31:33 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
You are very welcome. Please feel free to let us know if you have any more questions.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.