So in this JavaScript tutorial for beginners, I want to show you another kind of JavaScript event – the Window.onload event! The Window object in JavaScript represents the browser window of your web page, and the onload event is the completion of your web page loading.
Many times it’s useful to only start our JavaScript functions AFTER the window has fully loaded, so we can use this event to make sure that happens.
Any questions, fire away :).
SUBSCRIBE TO CHANNEL – https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg?sub_confirmation=1
========== JavaScript for Beginners Playlist ==========
========== CSS for Beginners Playlist ==========
========== HTML for Beginners Playlist ==========
========== The Net Ninja ============
For more front-end development tutorials & to black-belt your coding skills, head over to – https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg or http://thenetninja.co.uk
========== Social Links ==========
Twitter – @TheNetNinja – https://twitter.com/thenetninjauk
Original source
23 responses to “JavaScript Tutorial For Beginners #41 – Window onLoad Event”
Absolute legend mate. Thanks brother 🙂
where is the github for this lesson?
I don't know how this worked for you @NetNinja. I followed your tut to the point. However, only after adding the preventDefault(); to the code it worked for me? Are you working under other preconditions than I am?
best tutorials thanks shuan you awesome
Nice Job Mate
A little suggestion, can I?
It would be awesone if you give us your files to download, like the files on this vídeo, so we can download and use it to tests, like I don't know CSS and HTML yet, and looking to the codes on the files I would comprehend it better
i think when using defer attribute in <script src = xxx defer ></script> file should work without creating onload event
Can you please explain me why 'document.getElementById()' works only inside function 'setUpEvents' and not inside 'button.onclick fucntion()' ??
How did you know all this ??
my function consisting of actually 2 function doesnt work when i try it like this 🙁
Your instructions are so clear. Thank you for all of these awesome lessons!
Excellent tutorial, thank you!
Can't we say window.onload = setUpEvents(); on the last line?
Can you help me?
I am trying to use <Select> and <option in HTML and onchange in javascript to make an image change depending on the selection made in a drop down menu. When I change the drop down option, nothing happens..
MY HTML CODE:
<!DOCTYPE html>
<html>
<head>
<title>Detect Event</title>
<link rel="stylesheet" type="text/css" href="X_3_3.css">
<script src="X_3_3.js"></script>
</head>
<body>
<h3 class="headcolor">Exercise 3-3</h3>
<p>Select Gender: <br>
<p>
<select id="gender" name="gender" size="1">
<option value="none">None</option>
<option value="male">Male</option>
<option value="female">Female</option>
<option value="droid">Droid</option>
</select>
</p>
<div>
<img id="myimg" src="http://profperry.com/testimages/default.jpg" style="display: none;">
<img id="myimg" src="http://profperry.com/testimages/male.jpg" style="display: none;">
<img id="myimg" src="http://profperry.com/testimages/female.jpg" style="display: none;">
<img id="myimg" src="http://profperry.com/testimages/droid.jpg" style="display: none;">
</div>
</body>
</html>
MY JAVASCRIPT
var $ = function (id) {
return document.getElementById(id);
}
var alertGender = function()
{
var myGender = $("gender").value;
if (myGender == ("male")
{
$("myimg").src = "http://profperry.com/testimages/male.jpg";
}
if (myGender == Female)
{
$("myimg").src = "http://profperry.com/testimages/female.jpg";
}
if (myGender == Droid)
{
$("myimg").src = "http://profperry.com/testimages/droid.jpg";
}
window.onload = function ()
{
$("gender").onchange = alertGender;
}
Can you tell me why nothing happens?
Thank you.
Nice job thanks.
Really helpful. Can you please tell how does it differ from "onpageshow" event?
That last music blows my ears.
asdfdsasdf
Best javascript tutorials on the net…. though I would be using iife to accomplish this. :).
Keep up the great work Ninja!
i have 2 window.unload functions located in 1 js file. I need both to load on the same page (each have a different container name). Only 1 is loading. Why?
So in most cases, We don't use this technique, simply putting js link at the bottom of the document solves all the issues, is it not?
What's the difference between the way you set up those events? The last way you set it up is more code. I would prefer less. Does it actually matter?
Hello, I did the js code in separate file but it didn't display in the browser, is there some sitting that prevent js code from displaying in fire fox browser?