Reading web.config from JavaScript

Pass them in as variables from the view.

<script type="text/javascript">
   // needs to reside in your *.aspx file.
   $(function() {
       initPage(<%= Settings.Default.Inactivity %>, <%= Settings.Default.NoConfirm %>, <%= Settings.Default.LogoutUrl %>)
    });
    // Can reside in your *.aspx or in a *.js file.
    function initPage(inactivity, noconfirm, logoutUrl) {
        $.fn.idleTimeout = function(options) {
            inactivity: inactivity,
            noconfirm: noconfirm,
            logout_url: logoutUrl
        };
    };
</script>

Repeat for as many vars as you've got.


You can generate your JavaScript from ASP.NET.

Then simply write the settings at the server-side to your var defaults like this:

var defaults = {
    inactivity: <%=ConfigurationManager.AppSettings["Inactivity"] %>
}

EDIT:

If you want to keep your JavaScript in static js files, you can still initialize your var defaults from a small <script> rendered by your ASP.NET application. Your settings would be global, just like the AppSettings in web.config.


After to put the values on the config file, on the page that you will use the value put the java script this way bellow: You will access the value in the java script as a global, it is not necessary to declare it.

on the web config:

 </appSettings>
    <add key="varName" value="1" />
  </appSettings>

on the html page:

<script>
    var varName= '@System.Configuration.ConfigurationManager.AppSettings["varName"]';
</script>

It's not possible for directly accessing the config file from separate static JS file. One way for doing this is to add the java script in the .aspx page.

My Web Config:

<appSettings>
   <add key="test" value="textBox"/>
</appSettings>

My aspx page enter image description here

We are globally setting this variable so we can access this variable inside our static js file my js file home.js

function ReadConfigSettings()
{
   alert( test);
}

So we can read web config values from javascript by this way.