JavaScript block - how to render/place javascript on the right place in the .aspx page?

Posted on 2004-10-26
Last Modified: 2006-11-17

I have a simple javascript script that I want to render on .aspx page but on the right place.
Currently my javascript is located in the Page_Load() event and renders above the <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > which is wrong.

I would like script to be rendered within e.g. <HEAD> </HEAD> block.


<SCRIPT language="JavaScript">

Thank you,

Question by:morpheous_
    LVL 15

    Expert Comment

    Is there any reason not to just put it where you want it in the ASPX file (i.e., in the designer/HTML view)?

    Author Comment

    Yeah, I because I am performing operations where I measure time it takes to load c# code behind from the the Page_Load is started to the time .aspx content code is rendered.
    LVL 15

    Accepted Solution

    I'm not sure what that has to do with where the JS code lives, but....

    If you're currently building the <script>...</script> content as a string in Page_Load, one thing you can try is moving that code to a new property, say:

        protected string HideWaitJS() {
            get {
                // build your <script>...</script> in a string here
                return strJS;  // or whatever you call your string variable

    Then, in your ASPX, you can do:

        <%# HideWaitJS %>

    Finally, you'll need to call the page's DataBind() method somewhen -- typically in the Page_Load method -- to bind the <%#...%> site to the indicated property value.

    Alternately, you can use

        <%= HideWaitJS %>

    in which case no DataBind() call is necessary, and the <%=...%> simply translates to a Response.Write call during the Render phase.  (Both methods have their plusses and minuses, and their proponents and opponents.)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 ( But the ability to create custom scanning profiles al…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now