[Webinar] Streamline your web hosting managementRegister Today

x
  • 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?

Hello,


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.

javscript:

<SCRIPT language="JavaScript">
<!--
HideWait();
//-->
</SCRIPT>

Thank you,

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

    <head>
    <%# HideWaitJS %>
    </head>

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

    <head>
    <%= HideWaitJS %>
    </head>

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.)
0

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