By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,905 Members | 1,585 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,905 IT Pros & Developers. It's quick & easy.

Executing a JavaScript command from a web service

P: n/a
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

Feb 22 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,

go****@insightfulmes.com wrote:
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
Feb 22 '07 #2

P: n/a
On Feb 22, 1:49 pm, "Laurent Bugnion [MVP]" <galasoft...@bluewin.ch>
wrote:
Hi,

goo...@insightfulmes.com wrote:
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

Feb 23 '07 #3

P: n/a
Hi Mike,

Mike Buchanan wrote:
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
Feb 23 '07 #4

P: n/a
On Feb 23, 1:16 am, "Laurent Bugnion [MVP]" <galasoft...@bluewin.ch>
wrote:
Hi Mike,

Mike Buchanan wrote:
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

Feb 24 '07 #5

P: n/a
Hi,

Mike Buchanan wrote:
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/lib...5a(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/lib...z6(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
>
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

--
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
Feb 25 '07 #6

P: n/a
Hi again,
>public float Eval (string inExpression)
{
return eval(inExpression);
}
Here is an example which should help you do what you want:

http://www.odetocode.com/Code/80.aspx

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
Feb 25 '07 #7

P: n/a
On Feb 25, 11:43 am, "Laurent Bugnion [MVP]" <galasoft...@bluewin.ch>
wrote:
Hi again,
public float Eval (string inExpression)
{
return eval(inExpression);
}

Here is an example which should help you do what you want:

http://www.odetocode.com/Code/80.aspx

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
This does exactly what I want. Thanks for all your help.

Feb 27 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.