views:

42

answers:

4

Hi, i've added to the master page my script "myscript.js". Then, in a content page, i would like to load myscript() at startup (body onload).

How can i do this ?

Thank you in advance !

Regards

A: 

The form has onLoad event. So use the tag in your Masterpage

Saurabh
i don't want to use it in a masterpage to avoid run the script in other pages where it'snt necessary..
stighy
So why not Page.RegisterStartupScript or Page.RegisterClientScriptBlock?
Saurabh
I didn't kwnow them ;)
stighy
+1  A: 

Either use

Page.RegisterStartupScript

  if (!IsStartupScriptRegistered (key))
  {
      String script = "<script type=\"text/javascript\">" +
        "alert('Hello World');</" + "script>";
      RegisterStartupScript(key, script);
  }

Or you could use the JQuery library and add the function call to the document.ready function

http://jquery.com/

 $(document).ready(function(){
   // Add your function call here

 });
Daniel Dyson
A: 

I use a ContentPlaceHolder in the master-page for this purpose. This makes it possible to include specific scripts in the <head> only when you want it.

sshow
+1  A: 

this will add onload client side event to the master page's body tag:

in the master page:

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

in the content page:

HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("PageBody");

body.Attributes.Add("onload", "doSomething();");

HeathenWorld