I'm taking a introduction javascript class and for one of our labs we were instructed to have only ONE script tag in the head section but gets delayed by the window.onload event handler.
I got everything to work but my question is why do my imageManipulator() function only work when I do window.onload = function() { imageManipulator() }; and the other 2 works fine without the {}.
Also for my if statement to loop through the hockey teams why won't == 0 work? I thought 0 is true -1 is false so shouldn't it be == 0 and not >= 0?
My code is here:
<html>
<head>
<script>
function christmasDayCalculation()
{
var currentDate = new Date();
var christmas = new Date(2016, 11, 25);
var ms = christmas - currentDate;
var seconds = ms / 1000;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours / 24;
alert(Math.round(days) + " days until christmas");
}
function hockeyTeams()
{
var hockeyTeams = ['Anaheim Ducks', 'Arizona Coyotes', 'Calgary Flames', 'Edmonton Oilers',
'Los Angeles Kings', 'San Jose Sharks', 'Vancouver Canucks', 'Colorado Avalanch',
'Dallas Stars', 'Minnesota Wild', 'St.Louis Blues', 'Winnipeg Jets', 'Boston Bruins',
'Buffalo Sabres', 'Detroit Redw Wings', 'Florida Panthers', 'Montreal Canadiens',
'Ottowa Senators', 'Tampa Bay Lightning', 'Toronto Maple Leafs', 'Colombus Blue Jackets',
'New Jersey Devils', 'New York Islanders', 'New York Rangers', 'Philadelphia Flyers',
'Pittsburgh Penguins', 'Washington Capitals']
for(i = 0; i < hockeyTeams.length; i++)
{
if(hockeyTeams[i].indexOf("an") >= 0)
{
alert(hockeyTeams[i]);
}
}
}
function imageManipulator()
{
var numberOfImages = document.images.length;
for(i = 0; i < numberOfImages; i++)
{
document.images[i].style.border = "solid red"; // DOM 0 required per lab instructions
document.getElementsByTagName("img")[i].style.width = "100px"; // DOM 1 required per lab instructions
}
}
window.onload = christmasDayCalculation();
window.onload = hockeyTeams();
window.onload = function() { imageManipulator() };
</script>
</head>
<body>
<img src="cat.jpg">
<img src="dog.jpg">
<img src="bird.jpg">
</body>
</html>
Thanks!