473,498 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10613
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2057
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
3
28980
by: The_King | last post by:
Hello all: I was wondering if anyone has tried (and got to work) calling a c#.net ..dll from VBA code? I have been working on this for about 2 weeks and am not getting any where. The_King
2
9738
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
11
21887
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
2
3474
by: Pawan Aggarwal | last post by:
I'm having trouble with calling an exported function in a native DLL compiled with eMbedded Visual C++ in C# application in PocketPC 2002 OS. Problem Description follows: I have one exported...
1
12309
by: NOSPAM | last post by:
I have a question on calling parent class... What I want to do is: when calling the derived class, it automatically executes the parent's method, is it possible? // main code: Derived d = new...
0
1615
by: ajitgoel | last post by:
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...
1
2394
by: Memphis Steve | last post by:
Is it possible to combine multiple javascipts into one file and then call that file from a linked URL in the head section of an XHTML file? Here are the two scripts I want to use with the...
6
8530
by: sasha | last post by:
I have a c++ code that callls csharp. Now I want to be able to pass a function pointer from C++ to Csharp code and have c# callback to it. Is it possible and how? Here is what I have so far :...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.