Week 2 Lecture Slides
Week 2 Lecture Slides
Functions
JAVASCRIPT
Functions
INTERACTIVITY WITH
Functions
JAVASCRIPT
Functions
• Functions are bits of codes that you can reuse
• Functions have a special syntax
function functionName(parameters){
code you want to run
}
INTERACTIVITY WITH
Functions
JAVASCRIPT
Function Declaration
function welcomeMsg(msg){
alert(msg)
}
function welcomeMsg(){
alert("Welcome to JavaScript!")
}
INTERACTIVITY WITH
Functions
JAVASCRIPT
Function call
• Declaring a function doesn’t actual do anything
• You need to call the function
• Calling a function changes the program flow
INTERACTIVITY WITH
Functions
JAVASCRIPT
var x = "Hello"
welcomeMsg(x) function welcomeMsg(msg){
x = "Goodbye" alert(msg)
welcomeMsg(x) }
INTERACTIVITY WITH
Functions
JAVASCRIPT
Parameters
• Sometimes functions need some information in order to
perform its "function"
• The names of the parameters are not important, as long as
you are consistent
INTERACTIVITY WITH
Functions
JAVASCRIPT
Return values
• Some function return values
• These values can be used in assignment statements or
conditional expressions
INTERACTIVITY WITH
Functions
JAVASCRIPT
Return values
var firstName = welcomeMsg("Hi")
function welcomeMsg(msg){
alert(msg);
var name = prompt("What is your name?")
return name
}
INTERACTIVITY WITH
Functions
JAVASCRIPT
Review
• Whenever possible, use built in functions
• When you need to write your own function, try not to be too
specific
• Function parameters can have any name
INTERACTIVITY WITH
Functions
JAVASCRIPT
Code Placement
INTERACTIVITY
Code Placement WITH JAVASCRIPT
In the head
• When JavaScript functions are declared in the head section
they are separated from the content
• Use the script tag
• Have access to all of the document information (ids, classes,
etc.)
INTERACTIVITY
Code Placement WITH JAVASCRIPT
In an External File
• When JavaScript functions are in a separate file it is possible
to reuse the code in multiple files
• Don’t use the script tag
INTERACTIVITY
Code Placement WITH JAVASCRIPT
In an External File
<head>
<script src="js/two-external.js"></script>
</head>
<body>
<h1>Functions</h1>
<script>
message()
</script>
</body>
INTERACTIVITY
Code Placement WITH JAVASCRIPT
CodePen
• If you work on your code on an online editor (e.g. CodePen)
the software lets you separate HTML, CSS, and JavaScript
without any links
INTERACTIVITY
Code Placement WITH JAVASCRIPT
Review
• JavaScript can appear in the head or body of your code
• Code can also be placed in an external js file
• Personally, development done in head and moved to external
after testing
INTERACTIVITY
Code Placement WITH JAVASCRIPT
Folder Structure
• Web Developers tend to organize their code into separate
parts
○ HTML
○ CSS
○ Images
○ JavaScript
INTERACTIVITY
Folder Structure / Organizing Your Code
WITH JAVASCRIPT
Conventions
• Organizing your code is a convention, not a rule
INTERACTIVITY
Folder Structure / Organizing Your Code
WITH JAVASCRIPT
Debugging
• If a link isn’t working you want to check a few things:
○ Did you spell the file names correctly? (Case matters!!)
○ Are files in the correct folder?
○ Are you working on the correct file?
INTERACTIVITY
Folder Structure / Organizing Your Code
WITH JAVASCRIPT
Events
INTERACTIVITY
Events WITH JAVASCRIPT
Events
• onclick
○ User clicks on an HTML element
• onmouseover
○ User moves the mouse over an HTML element
• onresize
○ browser window is resized
• onload
○ browser finishes loading the page
INTERACTIVITY
Events WITH JAVASCRIPT
How it works
• Any element can react to an event.
• You need to add the event to the tag and include what you
want to happen
Using Quotes
• You can use single quotes or double quotes for the event
result
• Double quotes make it easier if you want to pass String
parameters
• Be careful of copying and pasting quotes!
Example
• Events – Basic Example
• Events – Basic Date Example
INTERACTIVITY
Events WITH JAVASCRIPT
More Events
• Mouse Events
○ onclick, ondblclick, onmousedown, onmouseenter,
onmouseleave, on mousemove, onmouseout,….
• Keyboard Events
○ onkeydown, onkeypress, onkeyup
• Frame Events
○ onload, onresize, onscroll, onerror,…
• Comprehensive list:
○ https://developer.mozilla.org/en-US/docs/Web/Events
INTERACTIVITY
Events WITH JAVASCRIPT
Review
• Without the events, JavaScript would be limited in ability to
interact with the DOM
• Events are cool….they are also annoying
• Don’t worry about memorizing the different events. As the
need arises, look them up
INTERACTIVITY
Events WITH JAVASCRIPT
Examples
• Events – Modify the DOM
• Events – Change Style
INTERACTIVITY
Code With Me: Events WITH JAVASCRIPT
Review
Stop and Code!
INTERACTIVITY
Code With Me: Events WITH JAVASCRIPT
“this”
INTERACTIVITY
“this”
WITH JAVASCRIPT
Referring to Elements
• A key to smart programming is using functions
• A common roadblock is figuring out how to set up functions
for reuse
○ How do I avoid writing a different function for every
different element?
○ How can the function know which one I want to use?
INTERACTIVITY
“this”
WITH JAVASCRIPT
“this”
• “this” is a keyword that allows an element to reference itself
○ Every object in the DOM has an automatically generated “this”
• Allows you to access an element’s info
○ Without “this” it would be difficult for the functions to know
what data to use
• “this” is also used outside functions
INTERACTIVITY
“this”
WITH JAVASCRIPT
Examples
• “this” Example – Simple
• “this” Example - Complex
INTERACTIVITY
“this”
WITH JAVASCRIPT
Review
• “this” is a tricky concept to grasp
• Repeated practice helps
• If you get stuck, work backward from where you see the
keyword find the last element that was started
INTERACTIVITY
“this”
WITH JAVASCRIPT
Photo Gallery
INTERACTIVITY
Photo Gallery WITH JAVASCRIPT
Example
• Gallery Homework
INTERACTIVITY
Photo Gallery WITH JAVASCRIPT
background-image
• The background-image is an option for including graphics
without using the img tag
• You should set a background-color as well in case the url isn’t
valid
background-image: url("mypPic.jpg")
background-color: #CCEECC;
INTERACTIVITY
Photo Gallery WITH JAVASCRIPT
Element text
• We have discussed two different ways to change the content
○ document.write()
○ innerHTML()
INTERACTIVITY
Photo Gallery WITH JAVASCRIPT
Tips
• The code should actually be quite short
• You will need to think about how to incorporate the quotes
• Remember, you use + to concatenate Strings
INTERACTIVITY
Photo Gallery WITH JAVASCRIPT