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,

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.