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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x