This project has moved. For the latest updates, please go here.

alert does not work ?

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

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.
Sybaris
Coordinator
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
Hi,

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

Sybaris
Coordinator
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!')");
Cheers!
Marked as answer by sybarisfr on 3/30/2016 at 10:56 PM
Sep 8, 2015 at 6:55 AM
Hi,

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

AddHostType instead of AddHostObject ...

Regards,

Sybaris