Executing Javascript on Page Load

J

Jonathan Wood

I've created some javascript and added it to my page (from a user control)
using Page.ClientScript.RegisterClientScriptBlock. The javascript hides and
shows certain elements in response to radio buttons being selected.

It seems to work fine except that I really need to run the script when the
page loads so the required elements are shown or hidden (some of these
elements do not have runat="server").

So, I use the following code:

Page.Form.Attributes.Add("onload", initFunction);

Where initFunction is something like "OnUserFood()".

This seems to have no effect. And looking at the resulting HTML, I notice
that my script appears within the form instead of in the header. I'm not
sure if this matters. But, although the onload attribute correctly appears
in the form tag, again, it doesn't appear to be actually making the call.

Any suggestions?

Thanks.
 
S

Scott Roberts

Jonathan Wood said:
I've created some javascript and added it to my page (from a user control)
using Page.ClientScript.RegisterClientScriptBlock. The javascript hides
and shows certain elements in response to radio buttons being selected.

It seems to work fine except that I really need to run the script when the
page loads so the required elements are shown or hidden (some of these
elements do not have runat="server").

So, I use the following code:

Page.Form.Attributes.Add("onload", initFunction);

Where initFunction is something like "OnUserFood()".

This seems to have no effect. And looking at the resulting HTML, I notice
that my script appears within the form instead of in the header. I'm not
sure if this matters. But, although the onload attribute correctly appears
in the form tag, again, it doesn't appear to be actually making the call.

Any suggestions?

Page.ClientScript.RegisterClientStartupScript
 
J

Jonathan Wood

Err... That would make sense, wouldn't it?

Haven't used that before but will investigate further.

Thanks!
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top