The document discusses JavaScript functions and event handling. It describes how functions can be defined and used to organize code. Event handling allows scripts to respond to user interactions, like clicking a button. When an event occurs, the browser calls the specified event handler function. This example uses a button click event to randomly roll dice images and update the page.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
22 views52 pages
Javascript Functions
The document discusses JavaScript functions and event handling. It describes how functions can be defined and used to organize code. Event handling allows scripts to respond to user interactions, like clicking a button. When an event occurs, the browser calls the specified event handler function. This example uses a button click event to randomly roll dice images and update the page.
All Rights Reserved. } You’ll combine new functions that you write with prepackaged functions and objects available in JavaScript } The prepackaged functions that belong to JavaScript objects (such as Math.pow, introduced previously) are called methods. } JavaScript provides several objects that have a rich collection of methods for performing common mathematical calculations, string manipulations, date and time manipulations, and manipulations of collections of data called arrays.
All Rights Reserved. } You can define programmer-defined functions that perform specific tasks and use them at many points in a script § The actual statements defining the function are written only once and are hidden from other functions } Functions are invoked by writing the name of the function, followed by a left parenthesis, followed by a comma-separated list of zero or more arguments, followed by a right parenthesis } Methods are called in the same way as functions, but require the name of the object to which the method belongs and a dot preceding the method name } Function (and method) arguments may be constants, variables or expressions
All Rights Reserved. } The script in our next example (Fig. 9.3) uses a programmer-defined function called maximum to determine and return the largest of three floating-point values.
All Rights Reserved. The body Element } The elements in the body are used extensively in the script. The form Element } The HTML5 standard requires that every form contain an action attribute, but because this form does not post its information to a web server, the string "#" is used simply to allow this document to validate. } The # symbol by itself represents the current page.
All Rights Reserved. The button input Element and Event-Driven Programming } Event-driven programming } the user interacts with an element in the web page, the script is notified of the event and the script processes the event. } The user’s interaction with the GUI “drives” the program. } The button click is known as the event. } The function that’s called when an event occurs is known as an event handler. } When a GUI event occurs in a form, the browser calls the specified event-handling function. } Before any event can be processed, each element must know which event-handling function will be called when a particular event occurs.
All Rights Reserved. The img Elements } The four img elements will display the four randomly selected dice. } Their id attributes (die1, die2, die3 and die4, respectively) can be used to apply CSS styles and to enable script code to refer to these element in the HTML5 document. } Because the id attribute, if specified, must have a unique value among all id attributes in the page, Java- Script can reliably refer to any single element via its id attribute. } Each img element displays the image blank.png (an empty white image) when the page first renders.
All Rights Reserved. Specifying a Function to Call When the Browser Finishes Loading a Document } Many examples will execute a JavaScript function when the document finishes loading. } This is accomplished by handling the window object’s load event. } To specify the function to call when an event occurs, you registering an event handler for that event. } Method addEventListener is available for every DOM node. The method takes three arguments: } the first is the name of the event for which we’re registering a handler } the second is the function that will be called to handle the event } the last argument is typically false—the true value is beyond this book’s scope
All Rights Reserved. } To show that the random values representing the dice occur with approximately equal likelihood, let’s allow the user to roll 12 dice at a time and keep statistics showing the number of times each face occurs and the percentage of the time each face is rolled (Fig. 9.6).
} which simulates the rolling of a six-sided die. This statement
always assigns an integer (at random) to variable face, in the range 1 £ face £ 6. } Referring to the preceding statement, we see that the width of the range is determined by the number used to scale random with the multiplication operator (6 in the preceding statement) and that the starting number of the range is equal to the number (1 in the preceding statement) added to Math.random() * 6.
All Rights Reserved. } We can generalize this result as face = Math.floor( a + Math.random() * b );
} where a is the shifting value (which is equal to the first
number in the desired range of consecutive integers) and b is the scaling factor (which is equal to the width of the desired range of consecutive integers).