how to insert JavaScript code dynamically...

G

Guest

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
 
G

Guest

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
 
S

S. Justin Gengo

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
 
G

Guest

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top