Javascript onload function

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
 
B

bruce barker

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

-- bruce (sqlwork.com)
 
M

Mark Rae [MVP]

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>";
 
G

Guest

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.
 

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,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top