how to insert JavaScript code dynamically...

Discussion in 'ASP .Net' started by =?Utf-8?B?SmFpbWUgU3R1YXJkbw==?=, Sep 12, 2005.

  1. 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
    =?Utf-8?B?SmFpbWUgU3R1YXJkbw==?=, Sep 12, 2005
    #1
    1. Advertising

  2. 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
    =?Utf-8?B?SmFzb24gTCBMaW5k?=, Sep 12, 2005
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    > 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
    S. Justin Gengo, Sep 12, 2005
    #3
  4. 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
    =?Utf-8?B?RWx0b24gVw==?=, Sep 12, 2005
    #4
  5. 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
    =?Utf-8?B?SmFpbWUgU3R1YXJkbw==?=, Sep 12, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MS News \(MS LVP\)
    Replies:
    0
    Views:
    596
    MS News \(MS LVP\)
    Aug 7, 2003
  2. cannontrodder
    Replies:
    1
    Views:
    711
    cannontrodder
    Jul 25, 2006
  3. Replies:
    1
    Views:
    436
  4. Replies:
    1
    Views:
    1,881
    Michael Nemtsev [MVP]
    Mar 17, 2008
  5. Sergio del Amo
    Replies:
    4
    Views:
    228
Loading...

Share This Page