Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Wouldn’t it be nice to be able to bring up a new animal and paragraph without having to reload the page each time? We can! We can place the.

Similar presentations


Presentation on theme: "Functions Wouldn’t it be nice to be able to bring up a new animal and paragraph without having to reload the page each time? We can! We can place the."— Presentation transcript:

1

2 Functions Wouldn’t it be nice to be able to bring up a new animal and paragraph without having to reload the page each time? We can! We can place the javascript inside a function, with a name, and then “call” the function again and again and again. Functions: giving code a name. Just like: “Get gas” is a shortcut for all the steps necessary to put gas in your car “Brush your teeth” again – shortcut for all the steps necessary to clean your teeth

3 Defining a function function fname () { code that function does } fname is the name you give your function. The { and } start and end the function You put whatever you want to happen in between { and }

4 Example: This is a paragraph function guessinggame() {var x = Math.floor(Math.random()*6) + 1 var y = prompt("Enter a number between 1 and 6") if (x == y) {document.getElementById("firstp").innerHTML = "Play the lottery!"; } else {document.getElementById("firstp").innerHTML = “You are wrong!"; } link

5 Add a button: This is a paragraph function guessinggame() {var x = Math.floor(Math.random()*6) + 1 var y = prompt("Enter a number between 1 and 6") if (x == y) {document.getElementById("firstp").innerHTML = "Play the lottery!"; } else {document.getElementById("firstp").innerHTML = “You are wrong!"; } link

6 Functions (continued) Remember, functions are a way of giving some code a name. Functions do not execute (run) automatically So far, the code we have written runs only when we click on the button Functions ONLY run when the code “calls” them Functions can go in the head section of our html code or in the body We usually put them in the head section Reason: if there are images involved, by putting it in the head section, the images will preload. If we put the code in the body section, the images won’t download until the function is called, making it run slow. Plus, it is just sloppier

7 Naming Functions You pick the name for your function Naming rules: Like variables: No spaces!!!!! No special characters Cannot start with a number Cannot be the same name as a variable you’re using Cannot be the same name as something that javaScript already uses.

8 function showconfirm() { var r=confirm("Press a button"); if (r==true) { document.getElementById('p1').innerHTML = "You pressed OK!"; } else { document.getElementById('p1').innerHTML = "You pressed Cancel!"; } Answer goes here link

9 function colorpref() { var color = prompt("Please enter your favorite color!", ""); if (color == "purple") { document.getElementById('p1').innerHTML="You are artistic and moody! " } else if (color == "blue") { document.getElementById('p1').innerHTML="You are serene and calm." } else if (color == "red") { document.getElementById('p1').innerHTML="You are fiery and passionate!" } else if (color == "green") { document.getElementById('p1').innerHTML="You are earthy and comfortable." } color transcript <input type = "button" value = "find out what your color says about you!" onClick = “colorpref()"> Your info will go here Thanks for playing! link

10 function coffeeinfo() { document.getElementById('p3').innerHTML = " The word 'coffee' was at one time a…. " document.getElementById('p3').style.color = "#CCBBAA" document.getElementById('p3').style.backgroundColor = "#995500“ } function teainfo() { document.getElementById('p3').innerHTML = " The origin of tea can be traced back to.. " document.getElementById('p3').style.color = "#227700" document.getElementById('p3').style.backgroundColor = "#BBCC22“ } link

11 Calling Functions (making them happen) There are a number of ways you can make a function happen in JavaScript You’ve seen onClick=“functionname()” There’s also: onMouseOver() – when you run your mouse over something onMouseOut() – when you take your mouse pointer off of something onLoad() – for when the web page loads

12 Calling Functions (making them happen) You’ve seen onClick=“functionname()” Also have: onDblClick() – when you double-click on something onFocus() – when you put your cursor into a form element like a textbox onBlur() – when your cursor leaves a form element onKeyDown () – when you press a key down over something onKeyUp() – when you release a key over something onKeyPress()- when you press and release a key over something onMouseDown()- when you click the mouse over something (but don’t release it) onMouseUp() – when you release the mouse over something onMouseMove()- moving the mouse while hovering over something onSubmit() – when submitting a form onUnload() – when you leave the current web page window you’re in.

13 onMouseOver, onMouseOut function changepara() { document.getElementById('firstp').innerHTML = "GET YOUR MOUSE OFF THAT BUTTON!" } function changethanks() { document.getElementById('firstp').innerHTML = "Whew, that was close!" } This is a very important paragraph!!! link

14 onMouseOver,onMouseOut function changepara() {document.getElementById('firstp').innerHTML = "DON'T RUN YOUR MOUSE OVER THIS PARAGRAPH!" } function changethanks() {document.getElementById('firstp').innerHTML = "Thank you for taking your mouse off this paragraph" } This is a very important paragraph!!! link

15 images function changepic() {document.getElementById('pic1').src = "Images/ghost.jpg" } function changeback() {document.getElementById('pic1').src = "Images/woman.jpg" } <img src = "Images/woman.jpg" width = "300" height = "300" id = "pic1" onMouseOver = "changepic()" onMouseOut = "changeback()"> link

16 Comments A way of including comments to yourself and to other people without the browser caring. Comments start with /* and end with */. Everything between these opening and closing markers is ignored by the browser so anything between them won’t be run by javascript. /* This script asks the user for a number. It uses that number to set the element with the id “ball1” to the width of the number the user entered */ var x = parseInt(prompt("What size should the ball's width be?")) document.getElementById("ball1").width = x Comments are designed for leaving information for people to read (as opposed to the browser) But we can use comments to isolate what code is working and what isn’t

17 Debugging: You've got a problem – your code doesn't work. There is a "bug“* in your code. (something that isn't correct Could be a typo (most likely!!) Could be a "syntax error" e.g., document.getElementbyId('pic1).src = "cat.jpg"; e.g., forgetting an opening or closing { } or ( ) e.g., if (par1 = 'pic1') Could be a logic error These are the hardest to find! When what you're trying to do won't be done in the way you're trying to do it. How do you find the "bug"?? *Aside: the term debugging came from radio repairmen from before WWII. In fixing radios, the repairmen often had to clean out bug carcases in order to get the radios working.

18 Finding the bug (Debugging) 1.Is your web site showing up as you want it to? Probably an html error 1.Make sure the page is valid (you’ve got an opening and closing tag and an opening and closing tag inside it. 2.Check to make sure that if you opened a tag, you closed it properly. Next, check to make sure everything opened is closed 1.Make sure your quotes (" ") open and close properly 1.Make sure the quotes are " " and not “” (from copying from ppt or word) 2.Go through and check for opening and closing () and {} 3.CHECK CAREFULLY FOR PROPER CAPITALIZATION 4.If nothing shows up, check to make sure you've properly entered,


Download ppt "Functions Wouldn’t it be nice to be able to bring up a new animal and paragraph without having to reload the page each time? We can! We can place the."

Similar presentations


Ads by Google