简体   繁体   中英

How to read Session timeout value from web.config using javascript in JS file

Can anyone please guide me to read the value of Session timeout value using javascript contained in a .js file.

I need to access the value in a javascript file and process the value.

I tried to follow the below approach in a test.js file but it is not working:

var sessionTimeout = <%= Session.Timeout %>;
alert(sessionTimeout);

Your problem is that .JS files are not executed so they do not contain the Session.Timeout variable.

You can do two things:

  1. Include your javascript directly in your ASP/ASPX page that does have the code being executed.
  2. Register your JS script in your code.

Registering your JS

See: http://msdn.microsoft.com/en-us/library/aa479011.aspx#aspnet-usingjavascript_topic07

Page.RegisterClientScriptBlock("MyScript", _
   "<script language=javascript src='MyJavaScriptFile.js'>")

I have share m code which I use for connection string from this code you can replace logic for session.

<script language="javascript" type ="text/javascript"> 
function ReadWebConfig()
{
    var strCon = '<%=ConfigurationManager.ConnectionStrings["MysqlCon"].ConnectionString %>'
    alert(strCon);
    var strTemp = '<%=ConfigurationManager.AppSettings["WordTemplate"].ToString() %>'
    alert(strTemp);
}
</script>

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