How do I call a JavaScript when the first page loads?

HLRosenberger
HLRosenberger used Ask the Experts™
on
How do I call a JavaScript when the first page loads?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I mean, when a page initially loads.
Lead Developer
Commented:
<body onload="js_function()";>
Greg AlexanderLead Developer

Commented:
Thats one way.. if you use jquery

$(document).ready(function(){
//javascript here
});
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Greg AlexanderLead Developer

Commented:
of course js_function() is whatever functon you want to run
Jquery way of doing: http://docs.jquery.com/Tutorials:Introducing_$(document).ready()


or use .onload
http://www.w3schools.com/jsref/event_body_onload.asp


<html>
<script language='javascript'>
function happycode(){
   alert('helo');
}
</script>
<body>
<h1>Javascript call after page loaded</h1>
 
<script>
//call after page loaded
window.onload=happycode ;
</script>
</body>
</html

Author

Commented:
galexander07:

<body onload="js_function()";>

This works!  Thanks.  Is there a way to ONLY call it on the first load, not on a postback?
Greg AlexanderLead Developer

Commented:
Not unless you register a cookie and check for it in the function or something of that nature

Author

Commented:
Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial