Executing a JavaScript command from a web service

G

google

I would like to execute a single JavaScript command from within a web
service. The command is a mathmatical statement.

I have a web page with some client side logic that does this and I'm
now moving that functionality into a web service.

To clarify - on the UI the user is collecting and entering data and
then it gets stored into the database. Most fields are input but
there are some that are calculated and stored. The calculated fields
use Java Script notation. For example the data collection might have
3 parameters, user inputs P1 and P2 and then a calculated field
storing the larger value. The calculation formula would be "Math.max
($P1$, $P2$)" The dollar signs means run-time substitution.

I can perform the string manipulation to get a statement like
"Math.max(10, 20)". Is there a way to call java to evaluate it and
get the returned value?

The existing UI piece has been in place for a long time so there are
many expressions already specified in Java syntax.

Mike Buchanan
 
L

Laurent Bugnion [MVP]

Hi,

I would like to execute a single JavaScript command from within a web
service. The command is a mathmatical statement.

I have a web page with some client side logic that does this and I'm
now moving that functionality into a web service.

To clarify - on the UI the user is collecting and entering data and
then it gets stored into the database. Most fields are input but
there are some that are calculated and stored. The calculated fields
use Java Script notation. For example the data collection might have
3 parameters, user inputs P1 and P2 and then a calculated field
storing the larger value. The calculation formula would be "Math.max
($P1$, $P2$)" The dollar signs means run-time substitution.

I can perform the string manipulation to get a statement like
"Math.max(10, 20)". Is there a way to call java to evaluate it and
get the returned value?

The existing UI piece has been in place for a long time so there are
many expressions already specified in Java syntax.

Mike Buchanan

First, let me precise that Java has nothing to do with JavaScript (or as
much as a car had to do with a carpet).

That said, your calculation was working on the client until now. Putting
it in a web service means moving it to the server. I assume that you use
ASP.NET?

ASP.NET allows programming in JScript.NET, which has an "eval" method
that you can use for this kind of purposes. A colleague of mine does
this and it works fine. So you can program your web service in
JScript.NET, or program it in C# and link to a JScript.NET utility
assembly which would evaluate the expression and return the result.

HTH
Laurent
 
M

Mike Buchanan

Hi,





I would like to execute a single JavaScript command from within a web
service. The command is a mathmatical statement.
I have a web page with some client side logic that does this and I'm
now moving that functionality into a web service.
To clarify - on the UI the user is collecting and entering data and
then it gets stored into the database. Most fields are input but
there are some that are calculated and stored. The calculated fields
use Java Script notation. For example the data collection might have
3 parameters, user inputs P1 and P2 and then a calculated field
storing the larger value. The calculation formula would be "Math.max
($P1$, $P2$)" The dollar signs means run-time substitution.
I can perform the string manipulation to get a statement like
"Math.max(10, 20)". Is there a way to call java to evaluate it and
get the returned value?
The existing UI piece has been in place for a long time so there are
many expressions already specified in Java syntax.
Mike Buchanan

First, let me precise that Java has nothing to do with JavaScript (or as
much as a car had to do with a carpet).

That said, your calculation was working on the client until now. Putting
it in a web service means moving it to the server. I assume that you use
ASP.NET?

ASP.NET allows programming in JScript.NET, which has an "eval" method
that you can use for this kind of purposes. A colleague of mine does
this and it works fine. So you can program your web service in
JScript.NET, or program it in C# and link to a JScript.NET utility
assembly which would evaluate the expression and return the result.

HTH
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog:http://www.galasoft-LB.ch
PhotoAlbum:http://www.galasoft-LB.ch/pictures
Support children in Calcutta:http://www.calcutta-espoir.ch- Hide quoted text -

- Show quoted text -

Thanks Laurent,

I don't have JScript.NET installed on my machine, but maybe I can get
it. The web service is already written in C# with this one little bit
stubbed out.

Mike Buchanan
 
L

Laurent Bugnion [MVP]

Hi Mike,

Mike said:
Thanks Laurent,

I don't have JScript.NET installed on my machine, but maybe I can get
it. The web service is already written in C# with this one little bit
stubbed out.

Mike Buchanan

The alternative would be to find a .NET JavaScript engine, but I don't
think there is one available, and honestly it would be quite overkill.
Thanks to the multi-language support in .NET, using JScript.NET for this
task seems more reasonable.

Let us know how that works out

Greetings,
Laurent
 
M

Mike Buchanan

Hi Mike,

Mike said:
Thanks Laurent,
I don't have JScript.NET installed on my machine, but maybe I can get
it. The web service is already written in C# with this one little bit
stubbed out.
Mike Buchanan

The alternative would be to find a .NET JavaScript engine, but I don't
think there is one available, and honestly it would be quite overkill.
Thanks to the multi-language support in .NET, using JScript.NET for this
task seems more reasonable.

Let us know how that works out

Greetings,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog:http://www.galasoft-LB.ch
PhotoAlbum:http://www.galasoft-LB.ch/pictures
Support children in Calcutta:http://www.calcutta-espoir.ch

I'm using Visual Studio 2003. I found JSharp.NET, is that what you
mean?

I installed it but without any Java experience or a text book I
haven't gotten very far.

I looked around for the "eval" method but didn't see it in the
language.

What I want *seems* pretty simple, a class with one public method that
takes a JavaScript numeric expression as a string, evaluates it and
returns a number. If I could find the "eval" function then it would
be something like:

public float Eval (string inExpression)
{
return eval(inExpression);
}

Mike Buchanan
 
L

Laurent Bugnion [MVP]

Hi,

Mike said:
I'm using Visual Studio 2003. I found JSharp.NET, is that what you
mean?

No, JSharp.NET is a Java-based language, and as we all know, Java has
nothing to do with JavaScript, which is what you want.
I installed it but without any Java experience or a text book I
haven't gotten very far.

I looked around for the "eval" method but didn't see it in the
language.

I looked around, and found this page, which should help you:
http://msdn2.microsoft.com/en-us/library/72bd815a(vs.71).aspx

The trick is that you cannot compile JScript.NET in Studio directly, you
need to compile it using the command line as explained here:
http://msdn2.microsoft.com/en-us/library/7435xtz6(VS.71).aspx

Once compiled, you get an assembly that you should be able to reference
like a normal .NET assembly. If I have time today, I'll try and write a
small example.

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top