0

I'm building a plain old HTML website that I'm putting a little Javascript into. Out of curiosity, I've been considering using other languages to accomplish the tasks that I want to achieve using Javascript, which include locating (and ordering) objects by date and tag variables.

Is it at all possible to incorporate C# in my JS code, referencing a library located somewhere in the site's directory? Would it theoretically be possible, or would I essentially just be translating the C# into the equivalent Javascript?

Wolfish
  • 960
  • 2
  • 8
  • 34
  • you can make ajax calls to a web api, but your c# code won't run in a javascript context – Deblaton Jean-Philippe Jul 17 '16 at 17:18
  • @DeblatonJean-Philippe So I'd essentially have to host the entire C# library somewhere as an API, then use some complicated referencing to make it have even a chance of working? – Wolfish Jul 17 '16 at 17:20
  • See http://stackoverflow.com/questions/36491385/how-is-asynchronous-javascript-interpreted-and-executed-in-node-js , http://stackoverflow.com/questions/37934181/in-the-v8-javascript-engine-how-do-you-add-a-functiontemplate-as-an-attribute-o – guest271314 Jul 17 '16 at 17:21

1 Answers1

2

Sorry, you can't. Browsers can only run Javascript code. There are other languages (coffeescript, typescript, etc) that "compile" to javascript, but in the end the only valid language for client side scripting is Javascript.

There are some "C# to javascript" translators, however that only gives you a similar syntax, but not the wide standard library that .NET Framework comes with.

What you can do however is run your C# code on server side (you can run ANYTHING there, since it's completely under your control) and then use AJAX to call it. This is known as a "web service" and it's a pretty standard practice.

Vilx-
  • 104,512
  • 87
  • 279
  • 422
  • So, assuming I built a small application that runs server-side, I could allow calls to it from Javascript, returning valid variables? Essentially, a glorified API with it's own executable context? – Wolfish Jul 17 '16 at 17:25
  • Yup. And, since it's on the server side, nobody sees what's going on. Good for security - you can do all kinds of stuff, connect to DB's, check credentials, etc - and nobody could possibly get access to anything that you didn't allow them to. (But be careful - make a mistake, and it's a security hole) – Vilx- Jul 17 '16 at 17:28
  • Ah. So I'd be spending more time replicating what is already provided by existing templates. – Wolfish Jul 17 '16 at 17:31
  • Huh? What do you mean? – Vilx- Jul 17 '16 at 17:52