Javascript: Events
ATLS 3020 - Digital Media 2 Week 7 - Day 2
Javascript: Events ATLS 3020 - Digital Media 2 Week 7 - Day 2 - - PowerPoint PPT Presentation
Javascript: Events ATLS 3020 - Digital Media 2 Week 7 - Day 2 Project Help: Case Sensitivity Use the toLowerCase() function Javascript var str = "This IS a Test"; alert(str); // This IS a Test var str_lower =
ATLS 3020 - Digital Media 2 Week 7 - Day 2
var str = "This IS a Test"; alert(str); // This IS a Test var str_lower = str.toLowerCase(); alert(str_lower); // this is a test Javascript
var answer; answer = prompt("Which do you choose: red or blue"); var answer_lower; answer_lower = answer.toLowerCase(); if(answer_lower == "red") { // do the thing for red } . . . Javascript
<body> . . . . <script> // code for your game // . . . do_x(); function do_x() { . . . } </script> </body> HTML
<body onload = "play_game();"> . . . . <script> function play_game() { // code for your game // . . . do_x(); } function do_x() { . . . } </script> </body> HTML
<body onload = "play_game();"> . . . . <script> function play_game() { // code for your game // . . . do_x(); } function do_x() { . . . } </script> </body> HTML
Webpage has finished loading
Browser has encountered an error
Browser window has been resized
The user has scrolled the webpage
User releases a key
User clicked mouse
User double clicked mouse
User moved the mouse
User moved mouse over element
Any input or textarea changed
Any form element changed
User submitted the form
User selected form element
❶ Access an html element and store it in a variable.
var start_div; start_div = document.getElementById("start"); javascript
❷ Use the dot notation to combine the variable and the event.
start_div. javascript
❸ Add the word “on” with the event you want to use. Add it to your element (after the dot).
start_div.onclick javascript
❹ Set the event equal to a function.
start_div.onclick = my_function; javascript
<div id="start">Start</div> <script> var start_div; start_div = document.getElementById("start"); start_div.onclick = do_this_function(); function do_this_function() { // code } </script> HTML <div id="start">Start</div> <script> var start_div; start_div = document.getElementById("start"); start_div.onclick = do_this_function(); function do_this_function() { // code } </script> HTML <div id="start">Start</div> <script> var start_div; start_div = document.getElementById("start"); start_div.onclick = my_function; function my_function() { // code } </script> HTML
The “onclick” event will be extremely helpful in future projects!
<div id="start">Start</div> <script> var start_div; start_div = document.getElementById("start"); start_div.onmouseover = my_function; function my_function() { // code } </script> HTML
“onmouseover” is another useful event
<img src="image.jpg" id="my_img" /> <div id="start">Start</div> <script> var start_div; start_div = document.getElementById("start"); start_div.onmouseover = my_function; function my_function() { var my_img = document.getElementById("my_img"); my_img.src = "new_image.jpg"; } </script> HTML