Vikram Ranabhatt
Vikram Ranabhatt

Reputation: 7620

Accessing ConfigurationManager.AppSettings in Java script

I had this javascript code in aspx file

Page.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"]%>']"

How to get the value from it.

Upvotes: 1

Views: 15552

Answers (1)

Satpal
Satpal

Reputation: 133423

<%=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.

You can do decalre a variable in aspx page

<script type="text/javascript">
    var GAAccountId = '<%=ConfigurationManager.AppSettings["GAAccountId"]%>';
</script>

Use GAAccountId variable in JS File

Upvotes: 5

Related Questions