Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access C# variable in javascript file .js

Is it possible to create a javascript file with some C# variables? Something like this:

var foo =  <%= CODE_VALUE.foo %>;

Or do I have to create them in a usercontrol.ascx which I include in the <head> section?

like image 782
Johan Avatar asked Dec 20 '22 22:12

Johan


1 Answers

You can not have this code inside the .js file because is not compile by asp.net and so is not translate the CODE_VALUE.foo into code.

what you can do is to place this variables just before load this .js file that you won to use, and set them on any aspx or user control.

For example

<script>
 var foo = <%=CODE_VALUE.foo%>;
<script>
<script type="text/javascript" src="fileToUseTheFoo.js">   
</script>

alternative you can create a handler that read javascript files, include your custom variables and create a full javascript code as you like, and send it as javascript one.

Some relative: How to get asp.net client id at external javascript file

like image 86
Aristos Avatar answered Jan 05 '23 06:01

Aristos