By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,581 Members | 2,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,581 IT Pros & Developers. It's quick & easy.

how to insert JavaScript code dynamically...

P: n/a
Hi all...

I need to add some JavaScript code that is dynamically generated to some
point of the page.

Currently I'm using
ClientScript.RegisterStartupScript(GetType(), "menu", "<script
type=\"text/javascript\">" + oMenu.BuildMenu(true, true, true) + "</script>");

But the script is added to the end of the page, but I need it to be added at
the begining of the page (after <BODY> tag).

How can I do it?
Thanks

Jaime
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Javascript can be run from any point on the page no matter where it is, so I
don't see why placement of it would be of any importance.

What I would do is place a Literal ASP Web Control right below where you
want to place it and then in the codebehind do this:
this.litJavaScript.Text = "<script
type=\"text/javascript\">Javascript</script>";

Hope that helps,

Jason Lind
"Jaime Stuardo" wrote:
Hi all...

I need to add some JavaScript code that is dynamically generated to some
point of the page.

Currently I'm using
ClientScript.RegisterStartupScript(GetType(), "menu", "<script
type=\"text/javascript\">" + oMenu.BuildMenu(true, true, true) + "</script>");

But the script is added to the end of the page, but I need it to be added at
the begining of the page (after <BODY> tag).

How can I do it?
Thanks

Jaime

Nov 19 '05 #2

P: n/a
Jaime,

You could use Page.RegisterClientScriptBlock instead, but I don't think that
will always insert the script where you want it either. Why does the script
need to go just after the body tag? Knowing that, there may be some other
solution to your problem.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Jaime Stuardo" <Ja**********@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
Hi all...

I need to add some JavaScript code that is dynamically generated to some
point of the page.

Currently I'm using
ClientScript.RegisterStartupScript(GetType(), "menu", "<script
type=\"text/javascript\">" + oMenu.BuildMenu(true, true, true) +
"</script>");

But the script is added to the end of the page, but I need it to be added
at
the begining of the page (after <BODY> tag).

How can I do it?
Thanks

Jaime

Nov 19 '05 #3

P: n/a
Hi Jaime,

One way to put Javascript code in specified position is to have a Label
control in the position, then assign its Text as Javascript code.

HTH

Elton Wang

"Jaime Stuardo" wrote:
Hi all...

I need to add some JavaScript code that is dynamically generated to some
point of the page.

Currently I'm using
ClientScript.RegisterStartupScript(GetType(), "menu", "<script
type=\"text/javascript\">" + oMenu.BuildMenu(true, true, true) + "</script>");

But the script is added to the end of the page, but I need it to be added at
the begining of the page (after <BODY> tag).

How can I do it?
Thanks

Jaime

Nov 19 '05 #4

P: n/a
In this case, placing the JavaScript anywhere on the page can have differents
results.

That javascript I want to embed in the page is only a function call that
draws a DHTML menu (programmed by a third party). The menu is drawn where
that function is called, using relative positioning

In the current scenario I have:

<form id="frmMain" runat="server">
<div>
<div style="z-index: 101; left: 0px; width: 100%; position: static;
overflow: visible; border-top-width: 5px; border-left-width: 5px;
border-left-color: green; border-bottom-width: 5px; border-bottom-color:
green; border-top-color: green; border-right-width: 5px; border-right-color:
green;">

SOME CONTENT

</div>
</div>
</form>

If I use RegisterStartupScript, the script is placed just before the closing
</form> tag. That causes the menu to appear after "SOME CONTENT". I need it
to be placed before it, so it would be sufficient if if is placed just after
the first <div> tag.

I hope it's clear now why I need to place a JavaScript code in a specific
location.

Jaime

"Jason L Lind" wrote:
Javascript can be run from any point on the page no matter where it is, so I
don't see why placement of it would be of any importance.

What I would do is place a Literal ASP Web Control right below where you
want to place it and then in the codebehind do this:
this.litJavaScript.Text = "<script
type=\"text/javascript\">Javascript</script>";

Hope that helps,

Jason Lind
"Jaime Stuardo" wrote:
Hi all...

I need to add some JavaScript code that is dynamically generated to some
point of the page.

Currently I'm using
ClientScript.RegisterStartupScript(GetType(), "menu", "<script
type=\"text/javascript\">" + oMenu.BuildMenu(true, true, true) + "</script>");

But the script is added to the end of the page, but I need it to be added at
the begining of the page (after <BODY> tag).

How can I do it?
Thanks

Jaime

Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.