Javascript onload function

  • Thread starter Thread starter Fresno Bob
  • Start date Start date
F

Fresno Bob

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
 
RegisterClientScriptBlock renders the script block just after the <form>
tag.

-- bruce (sqlwork.com)
 
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>";
 
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.
 
Back
Top