Follow below process to accomplish:
Step 1: Declare variable as public and static in code-behind file
public partial class Page1 : System.Web.UI
{
public string firstName = "Manas";
protected void Page_Load()
{
}
}
Step 2: Access the variable in js file:
<script>
GetMyName();
function GetMyName()
{
alert(<%=this.firstName%>);
}
</script>
Edit(Another way):
public partial class Page1 : System.Web.UI
{
public string firstName = "Manas";
protected void Page_Load()
{
string script = string.Format("var firstName = '{0}';", "Manas");
if (!ClientScript.IsClientScriptBlockRegistered("myScript"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "myScript", script, true);
}
}
}
Add code in temp.js file
<script>
GetMyName();
function GetMyName()
{
alert(firstName);
}
</script>