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.

Slides:



Advertisements
Similar presentations
function coffeeinfo() { document.getElementById('p3').innerHTML = "The word 'coffee' was.
Advertisements

Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
document.getElementById Used to change anything in your document (web page) that has an id Then you can change different aspects of the element with that.
Georgia Institute of Technology JavaScript part 2 Barb Ericson Georgia Institute of Technology May 2006.
JavaScript (Part 2) CS 183 4/13/2010. JavaScript (Part 2) Will cover: ◦ For.. In ◦ Events ◦ Try.. Catch, Throw ◦ Objects.
NAMEd anchors Enabling users to jump to specific points within Web documents.
20-Jun-15 JavaScript and HTML Simple Event Handling.
Information Technology Center Hany Abdelwahab Computer Specialist.
Computer Science 103 Chapter 4 Advanced JavaScript.
NAMEd anchors Enabling users to jump to specific points within Web documents.
JavaScript Client Side scripting. Client-side Scripts Client-side scripts, which run on the user’s workstation can be used to: Validate user inputs entered.
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
UNIT 6 JAVASCRIPT EVENT HANDLERS &WEB BROWSERS DETECTION.
JS: DOM Form Form Object Form Object –The Form object represents an HTML form. –For each instance of a tag in an HTML document, a Form object is created.
HTML Forms/Events (Instructor Lesson) The Event model HTML Forms Custom Events 1.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
Introduction to JavaScript. JavaScript Facts A scripting language - not compiled but interpreted line by line at run-time. Platform independent. It is.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Chapter 19: Adding JavaScript
Bridges To Computing General Information: This document was created for use in the "Bridges to Computing" project of Brooklyn College. You are invited.
Internet Mark-up Languages CO32036 Part Lecture: Elementary JavaScript.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
Unit 2, cont. September 12 More HTML. Attributes Some tags are modifiable with attributes This changes the way a tag behaves Modifying a tag requires.
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
 HTML is hypertext markup language. It is a way for people to display their information with more complex pictures and text displays. Before HTML, messages.
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
HTML Form Widgets. Review: HTML Forms HTML forms are used to create web pages that accept user input Forms allow the user to communicate information back.
1 JavaScript 2 JavaScript. 2 Rollovers Most web page developers first use JavaScript for rollovers A rollover is a change in the appearance of an element.
Event JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
IS2802 Introduction to Multimedia Applications for Business Lecture 3: JavaScript and Functions Rob Gleasure
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Making dynamic pages with javascript Lecture 1. Java script java versus javascript Javascript is a scripting language that will allow you to add real.
JavaScript Challenges Answers for challenges
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks.
COS 125 DAY 20. Agenda Assignment 8 not corrected yet Assignment 9 posted  Due April 16 New course time line Discussion on Scripts 
 Scripts that execute in response to some event  User clicking on something  Script does NOT execute as part of page loading  DOM facilities like.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Web Programming Java Script & jQuery Web Programming.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
5 th and 4 th ed: some from chapters 9, 12, 13 SY306 Web and Databases for Cyber Operations Slide Set #8: Dynamic HTML.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
SetTimeout()  What if you want to automatically cycle through the pics?  So you don’t have to keep clicking the button to see the next pic  Use the.
JavaScript Events.
JavaScript Event Handlers. Introduction An event handler executes a segment of a code based on certain events occurring within the application, such as.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
Event Handlers Events are asynchronous. When an event occurs, JavaScript can execute code in response to the user’s action. This response to the user-initiated.
Link. setTimeout() What if you want to automatically cycle through the pics? So you don’t have to keep clicking the button to see the next pic Use the.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
InnerHTML: This is a paragraph about Ted the cat What is the innerHTML of ‘cat’? zombies
JavaScript and HTML Simple Event Handling 11-May-18.
Web Development & Design Foundations with HTML5 7th Edition
JavaScript and HTML Simple Event Handling 19-Sep-18.
JavaScript Events.
JavaScript Defined General Syntax Body vs. Head Variables Math & Logic
JavaScript and Ajax (JavaScript Events)
Web Programming and Design
Barb Ericson Georgia Institute of Technology May 2006
Web Programming and Design
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

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

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 }

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

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

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

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.

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

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

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

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

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.

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

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

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

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

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.

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,