Javascript: Events and Functions
Javascript: Events and Functions
<img src=“logo.png”
onclick=“alert(‘You clicked me’)” >
¤Enables modularization
¤Ensures consistency
¤Modifications are only made once
Defining Functions
functionName (arguments);
Functions
<head>
<script src="script.js"></script>
</head>
Variable Scope
¤ Global variables
¤Convenient for accessing data throughout a
program.
¤Dangerous because it’s hard to keep track of
where they’re being changed.
¤ Use global variables sparingly and intentionally.
HTML Event Handlers
element.addEventListener(‘event’,
functionName, event flow[boolean]);
¤Event names don’t need to precede
with ‘on’
¤No parentheses after the function
name
¤Event flow is usually ‘false’
var
sw=document.getElementById("lo
go");
sw.addEventListener("click", changeImage,
Event Listeners with Parameters
var sw=document.getElementById("logo");
sw.addEventListener("click", function(){
changeImage("logo", "images/sw.jpg");
}, false);
Lab