alert does not work ?

Sep 7, 2015 at 11:48 AM
Edited Sep 7, 2015 at 11:48 AM

Why the following code crashs :
using (var engine = new Microsoft.ClearScript.Windows.JScriptEngine())
                engine.Execute("alert('hello world')");
Is there is a way to display messagebox like alert call ?

Thanks for advance.
Sep 7, 2015 at 3:31 PM
Hello Sybaris,

The alert method is part of the Web API, which is usually provided by web browsers and is not part of the JavaScript standard. However, you can easily expose a similar method based on WPF:
engine.Script.alert = new Action<string>(msg => MessageBox.Show(msg));
engine.Execute("alert('Hello, world!')");
Good luck!
Sep 7, 2015 at 8:29 PM

Thanks for your answer.
Is there is a way to declare same API alert (an action and not a function), but without using Script (dynamic) property ?

Something like that :
engine.AddHostType("alert", new Action<string>(msg => MessageBox.Show(msg)));
Note that previous code does not compile...
Thanks for advance

Sep 8, 2015 at 2:07 AM
Hi again,

Sure, you can do it this way:
engine.AddHostObject("alert", new Action<string>(msg => MessageBox.Show(msg)));
engine.Execute("alert('Hello, world!')");
Marked as answer by sybarisfr on 3/30/2016 at 10:56 PM
Sep 8, 2015 at 6:55 AM

Thanks. this is exactly the solution I was looking for.
I was not very far :-)

AddHostType instead of AddHostObject ...