1

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.

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

5 Comments

Any suggestion how to get value from it in java script file
Global means within <asp:Content ID="Content2" ..is it ? or in the page it self.I tried putting in Content2 it not accesible in java script.
Where do i put this golabl variable in aspx file
@Chris_vr, golabl variable in head section aspx file
No Not working.When I print that in console it says undefined.I have updated the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.