473,466 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Javascript onload function

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(GetTyp e(Page), "OnLoad",
sb.ToString)

End Sub
Oct 15 '07 #1
3 6936
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(GetTyp e(Page), "OnLoad",
sb.ToString)

End Sub

Oct 15 '07 #2
"Fresno Bob" <no****@nospam.comwrote in message
news:Oh**************@TK2MSFTNGP04.phx.gbl...
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

Oct 15 '07 #3

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(GetTyp e(Page), "OnLoad",
sb.ToString)

End Sub
Oct 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Ortt | last post by:
Hi Everyone, I have created a Javascript menu for my site which uses frames. The first stage loads fine but I want two drill down menus ("About Me Menu" and "Projects Menu"). The pages load...
3
by: John Ortt | last post by:
I appologise for reposting this but I have been trying to find a solution all week with no avail and I was hoping a repost might help somebody more knowledgable than myself to spot the message... ...
9
by: Robby Bankston | last post by:
I'm working on some code and am running into brick walls. I'm trying to write out Javascript with Javascript and I've read the clj Meta FAQ and didn't see the answer, read many similar posts (with...
9
by: beguigne | last post by:
Below is a snippet of a crude date picking routine for a form. I am a novice at this so, I am hitting my head at why when I select the day, the onChange event gives me a blank. What am I missing?...
6
by: sylcheung | last post by:
Hi, How can I be notified when the document load is complet in JavaScript? I am referring to the whold document load is complete, mean all images/external files/frame/iframes have been loaded. ...
4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
6
by: pronerd | last post by:
Hi, I am trying to dynamically set an event handler across frames. I have no problems setting properties across frames doing something like parent.ToolMenuFrame.location.href =...
8
by: Frank | last post by:
Hi, I am working with VS.NET 2005 Ultimately, I wish to call a JavaScript function from a .js file
1
by: zebra242 | last post by:
I'm working to get the onLoad function of a javascript to work: the script allows form buttons in html to refer to labeled frames in flash. Which works fine. Now I want to add onLoad...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.