Calling a Javascript function from CSharp class.

A

Ajit Goel

Hi;

We have a Javascript function which I have been tasked to move to a
CSharp class. This javascript function uses Regular expression
extensively. This function has a string input parameter and output
parameter. My questions are:

a. Can I expose a function in my CSharp code and internally call a
javascript function?? Is this a good idea??
b. Do we have any tool which can convert a javascript function into
Csharp code? I checked on the internet and could not find any tool which
could do that.

Any pointers would be helpful except probably rewriting the whole
function again in .Net.

I appreciate all the help.

Kind Regards;

Ajit Goel
 
E

Eric

a. Can I expose a function in my CSharp code and internally call a
javascript function?? Is this a good idea??

Your terminology is a bit strained. You expose a function that you
want to call from somewhere else, so it sounds like you want to expose
the JavaScript function and call it from C#? The answer is generally
no. Calling a client-side interpreted language from a compiled .NET
application doesn't seem like a good idea. But there are third-party
JavaScript interpreters that might support this, and even JavaScript
itself CAN be used locally. The question is really, "is this a good
idea" and I'd have to answer "no".
b. Do we have any tool which can convert a javascript function into
Csharp code?

I never heard of anything like that.

I assume you know about the RegEx support in C#? I bet the expression
syntax is very similar, if not identical.

Eric
 
S

shashank kadge

Your terminology is a bit strained. You expose a function that you
want to call from somewhere else, so it sounds like you want to expose
the JavaScript function and call it from C#? The answer is generally
no. Calling a client-side interpreted language from a compiled .NET
application doesn't seem like a good idea. But there are third-party
JavaScript interpreters that might support this, and even JavaScript
itself CAN be used locally. The question is really, "is this a good
idea" and I'd have to answer "no".


I never heard of anything like that.

I assume you know about the RegEx support in C#? I bet the expression
syntax is very similar, if not identical.

Eric

Assuming that you are working with a web application you can use
RegisterClientScriptBlock to register the javascript function from
server side.

-
shashank kadge
 
A

ajitgoel

Thanks Eric;

I guess I was not clear. We have a javascript file which has some
validation functions exposed. We need to provide interfaces for these
javascript methods through our .net classes(not through web pages,
where one would call a register the client script and work the
script.) These .net classes would act as just passthroughs to the
javascript code. This way the validation code would remain confined to
one javascript file.

The other way would be rewriting the whole javascript function\s
in .Net, which we want to avoid.

Kind Regards;

Ajit Goel
 
W

Willy Denoyette [MVP]

Thanks Eric;

I guess I was not clear. We have a javascript file which has some
validation functions exposed. We need to provide interfaces for these
javascript methods through our .net classes(not through web pages,
where one would call a register the client script and work the
script.) These .net classes would act as just passthroughs to the
javascript code. This way the validation code would remain confined to
one javascript file.

The other way would be rewriting the whole javascript function\s
in .Net, which we want to avoid.

JScript is a .NET managed language member , you can compile your JavaScript language code
into compiled code and call it from any other managed language.
Note that it might be necessary to change your code to take some advantage of the FCL, but
this should be a rather trivial task.

Willy.
 
A

ajitgoel

Thanks Willy;

I guess this might be a trivial question but how would the following
function look like in JScript.Net?? Would you have to use
Regex.Replace??

function fixbadcharjs4(s2f)
{
var s3f=s2f;
s3f=s3f.replace(/[\x27\xB4\'\']/gm,"`");
return(s3f);
}

This works in Javascript but I have yet to find a .Net equivalent.

Kind Regards;

Ajit Goel
 
W

Willy Denoyette [MVP]

Thanks Willy;

I guess this might be a trivial question but how would the following
function look like in JScript.Net?? Would you have to use
Regex.Replace??

function fixbadcharjs4(s2f)
{
var s3f=s2f;
s3f=s3f.replace(/[\x27\xB4\'\']/gm,"`");
return(s3f);
}

This works in Javascript but I have yet to find a .Net equivalent.

It's trivial, you function doesn't change JavaScript is a subset of JScript.
What you need is to include the function in a class and (preferably) your classes in a
package (namespace in .NET).

Here is a sample (using your function).

// test.js
import System;

// define a new package
package MyNamespace{

public class Test {
public function fixbadcharjs4(s2f)
{
var s3f=s2f;
s3f=s3f.replace(/[\x27\xB4\'\']/gm,"`");
return(s3f);
}
}
}

Compile this using jsc /t:library test.js

usage...

// C# file useTest.cs
....
static void Main()
//
{
MyNamespace.Teste = new MyNamespace.Test();
Console.WriteLine(e.fixbadcharjs4("whateverstringhere"));
...

compile above C# file using
csc /r:test.dll, system.jscript.dll usetest.cs

.... did not try it myself but it should run.


Willy.
 

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