Reputation: 7170
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 ?
Upvotes: 2
Views: 12052
Reputation: 469
Assign id and server="runnat" in master page body tag
<body runat="server" id="body_master">
Then your aspx .CS file insert below code in pageLoad event
HtmlGenericControl body = this.Master.FindControl("body_master") as HtmlGenericControl;
body.Attributes.Add("onLoad", "SessionTimeOuts();");
SessionTimeOuts() function should be defined in your .aspx file
Thank you
Upvotes: 0
Reputation: 117
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();");`
Upvotes: 2
Reputation: 9084
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.
Upvotes: 1
Reputation: 13230
Either use
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
$(document).ready(function(){
// Add your function call here
});
Upvotes: 3