[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

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


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,

  • 2
1 Solution
Is there any reason not to just put it where you want it in the ASPX file (i.e., in the designer/HTML view)?
morpheous_Author Commented:
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.
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.)

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now