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?

3
  • Seems like any solution would be dependent on includes and scripts initializing in a set order. Commented Mar 20, 2012 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 for ideas. Commented Mar 20, 2012 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. Commented Mar 21, 2012 at 20:14

1 Answer 1

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.