Server variables and the .js file. Is it possible?

2k Views Asked by At

is it possible to invoke server variables in the external .js file ?

[Edit] OK, I resolved the problem, thank for Your help ! :)

3

There are 3 best solutions below

0
On

No, internal JS you can

<script>
var i = <%= ServerSideVar %>
</script>
2
On

When you say, "invoke a server variable" do you mean you want to change the value of a variable or did you mean to say, "invoke a function on the server?"

This can be done using an ajax call back to the server (assuming you are trying to avoid a postback).

0
On

Use Page.ClientScript.RegisterStartupScript() or Page.ClientScript.RegisterClientScriptBlock() to register the value of your server variables to JavaScript variables and then you can call them from the external .js file

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "variables", String.Format("var var1 = {1}; var var2 = {2};", someVariable1, someVariable2));