javascript 在 Java 脚本中访问 ConfigurationManager.AppSettings
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18490503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing ConfigurationManager.AppSettings in Java script
提问by Chris_vr
I had this javascript code in aspx file
我在aspx文件中有这个javascript代码
Page.aspx
页面.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="server">
----some code----
added after suggestion from Satpal
<script type="text/javascript">
var GAAccountId = '<%=ConfigurationManager.AppSettings["GAAccountId"]%>';
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%--<script type="text/javascript">
debugger;
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<%=ConfigurationManager.AppSettings["GAAccountId"]%>']);
_gaq.push(['_setDomainName', 'auto']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview', '/Register']);
_gaq.push(['_trackPageLoadTime']);
(function () {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>--%>
</asp:Content>
Now I moved this code to seprate .js file and included the reference in javascript file.Now I am not able to access it.it is coming as it "'<%=ConfigurationManager.AppSettings["GAAccountId"]%>']"
现在我将此代码移到单独的 .js 文件中,并将引用包含在 javascript 文件中。现在我无法访问它。它即将到来 "'<%=ConfigurationManager.AppSettings["GAAccountId"]%>']"
How to get the value from it.
如何从中获取价值。
回答by Satpal
<%=ConfigurationManager.AppSettings["GAAccountId"]%>
will work when you are using it in aspx
file. It will not be evaluated when you are using it in external js file.
<%=ConfigurationManager.AppSettings["GAAccountId"]%>
当您在aspx
文件中使用它时会起作用。当您在外部 js 文件中使用它时,它不会被评估。
You can do decalre a variable in aspx page
您可以在 aspx 页面中对变量进行 decalre
<script type="text/javascript">
var GAAccountId = '<%=ConfigurationManager.AppSettings["GAAccountId"]%>';
</script>
Use GAAccountId
variable in JS File
GAAccountId
在 JS 文件中使用变量