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
morpheous_Asked:
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.

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

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
C#

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.