简体   繁体   中英

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?

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM