2

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?

Kerry
  • 143
  • 9
  • Seems like any solution would be dependent on includes and scripts initializing in a set order. – Mike Guthrie Mar 20 '12 at 16:39
  • Expanding on my previous comment, are you looking for a solution that only applies to your specific case? Seems whatever you build won't be very reusable. If that is acceptable, then give us some info on your site so we can come up with solutions. Such as is there a single common master page? You could also review [this question](http://stackoverflow.com/questions/220231/accessing-http-headers-in-javascript) for ideas. – Mike Guthrie Mar 20 '12 at 16:58
  • To clarify, the javascript is being called in a MasterPage. It opens a new window passing the URL and some querystring args. The URL and args are different in each environment, so I'd prefer to have these values in my web.config file. – Kerry Mar 21 '12 at 20:14

1 Answers1

1

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;
    }
}
Pankaj
  • 9,749
  • 32
  • 139
  • 283