Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading web.config from JavaScript

IS there any way that I can read config values in web.config with javascript ? Why would I want to do that ?

I have a timer in my website which would pop up a modal dialog with a count down timer (count down for 2 minutes) if the user is inactive for 20 minutes. If the user does not respond, it logs him out. If he does, it pings to the server (to maintain the session) and keeps the session alive

This 15 minutes is hardcoded in the js file. I would rather want to pick it up from a config file/some other file than having it hard coded into the JS

here is the code snippet

$.fn.idleTimeout = function(options) {         var defaults = {                     //I would like to pick these values from some config file             inactivity: 900000, //15 minutes              noconfirm: 120000, //2 minutes             sessionAlive: 900000, //15 minutes             click_reset: true,             logout_url: '/Views/Pages/Timeout.aspx'          } 

Any suggestions?

Edit: This is in a separate js file. Doing <%=%> would give error "illegal XML character [Break on this error] inactivity: <%=ConfigurationManager.AppSettings["Inactivity"] %>;"

like image 488
ram Avatar asked Dec 15 '09 20:12

ram


People also ask

Can JavaScript read web config?

<add key="serverip" value="###. ##. #. ##"/>

How read config file in JavaScript?

var Server = require('mongodb'). Server; var MongoDB = require('mongodb'). Db; var dbPort = readConfig(dbPort); var dbHost = readConfig(dbHost); var dbName = readConfig(dbName);

How do you access Appsettings in JavaScript?

It is not possible to read the AppSetting value directly in JavaScript and hence using JavaScript XmlHttpRequest (XHR), an AJAX call will be made to a WebMethod which will read the AppSetting value and return it to the JavaScript function.

How do I open web config file in Visual Studio?

Click Add to create the file and open it for editing. The file contains the code shown in the "Example" section later in this topic, with a few initial defaults. Your application inherits all configuration settings from the Machine. config and Web.


2 Answers

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.

like image 50
Daniel Vassallo Avatar answered Oct 02 '22 21:10

Daniel Vassallo


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.

like image 42
Jarrett Meyer Avatar answered Oct 02 '22 22:10

Jarrett Meyer