如何调用从web.config添加键值到javascript(.js)文件? [英] How to call add key values from web.config to javascript(.js) file ?

查看:210
本文介绍了如何调用从web.config添加键值到javascript(.js)文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将System.Configuration.ConfigurationManager.AppSettings [x]调用到javascript文件中?



我尝试了什么:



尝试配置管理器和其他参数不成功。

解决方案

你不能调用服务器直接代码,你需要你的服务器代码来渲染一个包含你需要的数据的javascript变量然后使用它



在你的aspx页面上(或者查看)如果你正在使用MVC)



< script> 
var mySetting ='<%= System.Configuration.ConfigurationManager.AppSettings [MySetting]%>';
< / script>
< script src =/ scripts / test.js>< / script>





test.js



 


(document).ready(function(){
if(typeof) (mySetting)==undefined){
//如果您需要检查变量是否已定义,您可以执行此操作
return;
}

//即使你的aspx页面上定义了mySetting,你仍然可以在js文件
//中使用它,只要在定义变量后包含它们
alert(mySetting);
});


How to call System.Configuration.ConfigurationManager.AppSettings["x"] in to javascript file ?

What I have tried:

Tried with config manager and other parameters not succeed.

解决方案

You can't call server-side code directly, you'll need your server code to render a javascript variable that has the data you need and then use that

On your aspx page (or view if you're using MVC)

<script>
    var mySetting = '<%=System.Configuration.ConfigurationManager.AppSettings["MySetting"]%>';
</script>
<script src="/scripts/test.js"></script>



test.js


(document).ready(function () { if (typeof (mySetting) == "undefined"){ // if you need to check if the variable has been defined you can do this return; } // even though mySetting is defined on your aspx page you can still use it in your js files // as long as they are included after the variable is defined alert(mySetting); });


这篇关于如何调用从web.config添加键值到javascript(.js)文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆