473,791 Members | 2,933 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 10632
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_...@ya hoo.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
RegisterClientS criptBlock 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_...@ya hoo.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.goo glegroups.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(s 2f)
{
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.bewro te:
<ajitg...@gmail .comwrote in message

news:11******** **************@ j27g2000cwj.goo glegroups.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.goo glegroups.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(s 2f)
{
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(s 2f)
{
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.Tes te = new MyNamespace.Tes t();
Console.WriteLi ne(e.fixbadchar js4("whateverst ringhere"));
...

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
2087
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 http://support.microsoft.com/default.aspx?scid=kb;EN-US;246800 function fxnParseIt() { var sInputString = 'asp and database';
3
29021
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
9784
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 towards this approach such as how would it works in multi-thread. Thanks Tony
11
21933
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 C# equivalent. Any ideas? -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc. varnk@diebold.com
2
3488
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 function in the DLL: I am making an application for pocket Pc in C# using this DLL Function. whenever I execute this exe in Pocket pc it returns MissingMethod Exception.
1
12327
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 Derived(); d.SomeMethod(); public class Base
0
1641
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 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
1
2416
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 instructions on where to normally place them in an XHTML document: SCRIPT #1: Step 1: Insert the below anywhere into the <body> section of your page where you wish the clock to be displayed:<script> /* Live Date Script-
6
8546
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 : #include "windows.h" #include <stdio.h> #import "CSDll.tlb" named_guids int main(int argc, char* argv)
0
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7537
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.