user710502
user710502

Reputation: 11469

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?

Upvotes: 0

Views: 4790

Answers (4)

rtpHarry
rtpHarry

Reputation: 13125

You can use the RegisterStartupScript to register your javascript snippets from the code behind:

Upvotes: 0

Larry Hipp
Larry Hipp

Reputation: 6255

You can use the HTML body onload event.

or

Use a javascript framework like jQuery and use the $(document).ready() event. .ready()

Upvotes: 0

Pinchy
Pinchy

Reputation: 1536

<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>

Upvotes: 7

Darkwing
Darkwing

Reputation: 7595

Add an onload event to the body html element.

http://www.w3schools.com/jsref/event_body_onload.asp

Upvotes: 0

Related Questions