Kerry
Kerry

Reputation: 143

asp.net Can I inject configuration settings into javascript?

I have a page that uses an external javascript file. That file requires variables that are different in Dev, QA and production environments, causing me to need to maintain multiple copies of the same script file for each environment.

I'd prefer to maintain the values of these variables in web.config (perhaps appSettings section), and resolve these values at runtime, before streaming the .js file to the browser. Is there a way to do this?

Upvotes: 2

Views: 1111

Answers (1)

Pankaj
Pankaj

Reputation: 10105

asp.net Can I inject configuration settings into javascript?

Sample Java Script

<script language="javascript" type="text/javascript">
    var Publicvalue = abc();
    function abc() {
        Publicvalue = <%=MyProperty%>
        alert(Publicvalue);
        return Publicvalue;
    }
</script>

Sample HTML

<asp:Button ID="btn" runat="server" Text="efeded" OnClientClick="return abc();" OnClick="btn_Click" />

Sample Code Behind

public int MyProperty
{
    get
    {
        return 1;
    }
}

Upvotes: 1

Related Questions