I'm a newbie and i'm playing around trying to achieve something using javascript.
I have a function which i need to take effect once my page loads but i don't want to use <body onload="init()"
is there another alternative?
<script type="text/javascript">
function init() {
container = document.createElement('div');
<script>
<body onload="init()">
</body>
I'm a newbie and i'm playing around trying to achieve something using javascript.
I have a function which i need to take effect once my page loads but i don't want to use <body onload="init()"
is there another alternative?
<script type="text/javascript">
function init() {
container = document.createElement('div');
<script>
<body onload="init()">
</body>
Share
Improve this question
asked Nov 20, 2015 at 19:06
ChimaChima
1821 gold badge1 silver badge16 bronze badges
4
- 1 You can use jQuery, and use the $(document).ready(handler) function, which will execute the function handler when the DOM is loaded. – user3186219 Commented Nov 20, 2015 at 19:07
- You should make it happend right in your javascript. Take a look here – AndreaM16 Commented Nov 20, 2015 at 19:12
- 1 Possible duplicate of Difference between DOMContentLoaded and Load events – Matías Fidemraizer Commented Nov 20, 2015 at 19:12
- 1 @ZachPerkitny - the document ready is a different event than window.load and is fired earlier. – Mark Schultheiss Commented Nov 20, 2015 at 19:22
4 Answers
Reset to default 10Call it from a load
handler:
function init() {
container = document.createElement('div');
// ...
}
window.addEventListener("load", init);
You can also do:
window.onload = function() {
init();
}
Well, you can use jquery, but you have to include it in your head manaully or with
<!-- Google CDN -->
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
if you want to use Google CDN, or
<!-- Microsoft CDN -->
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>
</head>
for Microsoft CDN. Now, you can use jquery in your webpage. So write in your head
<script type="text/javascript">
$(window).on('load",function(){
// your code
});
</script>
Of course, you need to know how to use jquery to use this solution. However, you can use javasript too, but I suggest you to start learning jquery: is very useful.
<script>
window.onload = function() {
//your code
}
</script>
This should be the right way ;)
REALLY IMPORTANT: this functions are called when your document is starting loading! If you want to apply something at the end of the loading of the page (better solution to improve loading speed) use
$(document).ready(function(){
//your code
});
in jquery, or
document.addEventListener("DOMContentLoaded", function(event) {
//your code
});
in javascript
I am new in programming but i think u can do something like this: With Jquery
$(document).ready(function(){
// code loaded..
});
Without Jquery
// this code is bad to practise
<script type="text/javascript">
function init() {
// Put your code here
}
window.onload = init;
</script>