468,505 Members | 1,561 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

Calling a Javascript function from CSharp class.

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

*** Sent via Developersdex http://www.developersdex.com ***
Mar 12 '07 #1
6 10281
On Mar 12, 12:41 pm, Ajit Goel <ajitg...@gmail.comwrote:
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

Mar 12 '07 #2
On Mar 12, 1:03 pm, "Eric" <englere_...@yahoo.comwrote:
On Mar 12, 12:41 pm, Ajit Goel <ajitg...@gmail.comwrote:
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
Assuming that you are working with a web application you can use
RegisterClientScriptBlock to register the javascript function from
server side.

-
shashank kadge

Mar 12 '07 #3
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

On Mar 12, 11:03 am, "Eric" <englere_...@yahoo.comwrote:
On Mar 12, 12:41 pm, Ajit Goel <ajitg...@gmail.comwrote:
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

Mar 12 '07 #4
<aj******@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
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.

Mar 12 '07 #5
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

On Mar 12, 2:43 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
<ajitg...@gmail.comwrote in message

news:11**********************@j27g2000cwj.googlegr oups.com...
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.

Mar 12 '07 #6
<aj******@gmail.comwrote in message
news:11**********************@n33g2000cwc.googlegr oups.com...
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("whateverstringh ere"));
...

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

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

Mar 12 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by The_King | last post: by
1 post views Thread by NOSPAM | last post: by
6 posts views Thread by sasha | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.