Run a Javascript On Page Load
I would like to开发者_开发百科 know how to run a javascript function on page load.. in my asp.net page. I do not want to do it in from my .cs file OnLoad, I want to keep it in the aspx page if at all possible.
I am new to javascript so any ideas?
<html>
<head>
<script type="text/javascript">
function loadJavaScript()
{
// add code here
}
</script>
</head>
<body onload="loadJavaScript()">
<h1>Java script on load demo</h1>
</body>
</html>
There is a nice example here
or
<script type="text/javascript">
$(document).ready(
function() {
//your code here
});
</script>
Add an onload event to the body html element.
http://www.w3schools.com/jsref/event_body_onload.asp
You can use the HTML body onload event.
or
Use a javascript framework like jQuery and use the $(document).ready() event. .ready()
You can use the RegisterStartupScript
to register your javascript snippets from the code behind:
- http://msdn.microsoft.com/en-us/library/bb310408.aspx
精彩评论