Javascript onload function

Discussion in 'ASP .Net' started by Fresno Bob, Oct 15, 2007.

  1. Fresno Bob

    Fresno Bob Guest

    I have a page which is a master page. I want to trigger a Javascript
    function in the onload event of the body. Simply attaching an onload event
    to the body which points to javascript function causes a javascript error
    (Object not found on line 0). I read that ASP.net does not like you doing it
    this way so I was looking at the ClientScript.RegisterClientScriptBlock.
    This doesn't write anything. I have tried putting it in the page load event
    of the master page and main page. What am I doing wrong. And also how do I
    get the the injected javascript to be called in the on load function of the
    page?

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load

    Dim sb As New StringBuilder

    sb.Append("<script language=""JavaScript"" type=""text/javascript"">" &
    Environment.NewLine)

    sb.Append("<!--" & Environment.NewLine)

    sb.Append("alert('test')" & Environment.NewLine)

    sb.Append(" -->" & Environment.NewLine)

    sb.Append("</script>" & Environment.NewLine)



    Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "OnLoad",
    sb.ToString)



    End Sub
     
    Fresno Bob, Oct 15, 2007
    #1
    1. Advertising

  2. Fresno Bob

    bruce barker Guest

    RegisterClientScriptBlock renders the script block just after the <form>
    tag.

    -- bruce (sqlwork.com)



    Fresno Bob wrote:
    > I have a page which is a master page. I want to trigger a Javascript
    > function in the onload event of the body. Simply attaching an onload event
    > to the body which points to javascript function causes a javascript error
    > (Object not found on line 0). I read that ASP.net does not like you doing it
    > this way so I was looking at the ClientScript.RegisterClientScriptBlock.
    > This doesn't write anything. I have tried putting it in the page load event
    > of the master page and main page. What am I doing wrong. And also how do I
    > get the the injected javascript to be called in the on load function of the
    > page?
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    > Handles Me.Load
    >
    > Dim sb As New StringBuilder
    >
    > sb.Append("<script language=""JavaScript"" type=""text/javascript"">" &
    > Environment.NewLine)
    >
    > sb.Append("<!--" & Environment.NewLine)
    >
    > sb.Append("alert('test')" & Environment.NewLine)
    >
    > sb.Append(" -->" & Environment.NewLine)
    >
    > sb.Append("</script>" & Environment.NewLine)
    >
    >
    >
    > Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "OnLoad",
    > sb.ToString)
    >
    >
    >
    > End Sub
    >
    >
     
    bruce barker, Oct 15, 2007
    #2
    1. Advertising

  3. "Fresno Bob" <> wrote in message
    news:...

    > And also how do I get the the injected javascript to be called in the
    > onload function of the page?


    <body id="objBody" runat="server">

    ((HtmlGenericControl)Master.FindControl("objBody")).Attributes["onload"] =
    "<script goes here>";


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Oct 16, 2007
    #3
  4. HtmlControl body = (HtmlControl)Master.FindControl("Body");
    body.Attributes.Add("onload", "callJavascript();");

    here, callJavascript();"); function may be on your current content page or
    any JS file.


    "Fresno Bob" wrote:

    > I have a page which is a master page. I want to trigger a Javascript
    > function in the onload event of the body. Simply attaching an onload event
    > to the body which points to javascript function causes a javascript error
    > (Object not found on line 0). I read that ASP.net does not like you doing it
    > this way so I was looking at the ClientScript.RegisterClientScriptBlock.
    > This doesn't write anything. I have tried putting it in the page load event
    > of the master page and main page. What am I doing wrong. And also how do I
    > get the the injected javascript to be called in the on load function of the
    > page?
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    > Handles Me.Load
    >
    > Dim sb As New StringBuilder
    >
    > sb.Append("<script language=""JavaScript"" type=""text/javascript"">" &
    > Environment.NewLine)
    >
    > sb.Append("<!--" & Environment.NewLine)
    >
    > sb.Append("alert('test')" & Environment.NewLine)
    >
    > sb.Append(" -->" & Environment.NewLine)
    >
    > sb.Append("</script>" & Environment.NewLine)
    >
    >
    >
    > Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "OnLoad",
    > sb.ToString)
    >
    >
    >
    > End Sub
    >
    >
    >
     
    =?Utf-8?B?VWp2YWwgU2hhaA==?=, Oct 16, 2007
    #4
    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. Fabio R.
    Replies:
    0
    Views:
    806
    Fabio R.
    Mar 25, 2005
  2. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    566
    Martin Honnen
    Nov 4, 2004
  3. Replies:
    5
    Views:
    267
    Thomas 'PointedEars' Lahn
    May 15, 2005
  4. lwhitb1
    Replies:
    1
    Views:
    110
    Evertjan.
    Nov 22, 2005
  5. marco

    onload -->* no onload

    marco, Jun 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    209
    marco
    Jun 24, 2006
Loading...

Share This Page