This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:web_development:text:javascript [2021/11/22 06:53] mag created |
en:web_development:text:javascript [2023/08/16 09:33] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Web Development Lesson 2 - Text ====== | ====== Web Development Lesson 2 - Text ====== | ||
===== Javascript ===== | ===== Javascript ===== | ||
+ | ==== Objectives ==== | ||
+ | In this exercise you'll get your first taste of Javascript and use it to create a simple game, where you get a user to change key words in your story to make it funny. | ||
==== Setup ==== | ==== Setup ==== | ||
We'll build on the work you've done so far so if you need to, copy the code below into [[https:// | We'll build on the work you've done so far so if you need to, copy the code below into [[https:// | ||
Line 11: | Line 13: | ||
<p style=" | <p style=" | ||
On Sundays they drive to the < | On Sundays they drive to the < | ||
+ | </ | ||
+ | |||
+ | ==== Preview ==== | ||
+ | We're going to turn this story into a game. You may have played this as a child. The user will be asked to provide words fitting certain categories, which will be placed into the story to make something ridiculous. Take a look at the final result here -> https:// | ||
+ | |||
+ | ==== Comments ==== | ||
+ | * We'll do this using Javascript, which is code that runs on the user's computer. | ||
+ | * Add the following code to the Javascript pane in the bottom left of your jsfiddle screen. | ||
+ | < | ||
+ | |||
+ | // collect input from user | ||
+ | |||
+ | // show input in story | ||
+ | |||
+ | // make background white to show text again</ | ||
+ | * The < | ||
+ | * These comments show the process we'll follow for our game. | ||
+ | * First, we will make the screen black to hide the text. | ||
+ | * Then we will collect input from the user and place it into the story. | ||
+ | * Finally, we'll make the screen white so the user can read the text. | ||
+ | |||
+ | ==== Object Notation ==== | ||
+ | * Look at the first comment. We want to add code below this to make the background black. | ||
+ | * If we were doing this in HTML directly, we'd add a style attribute to the body tag like this. < | ||
+ | * In Javascript, we'll do the same by modifying that attribute. | ||
+ | * Add the following text below the first comment. This text is an instruction to the browser, usually referred to as a ' | ||
+ | < | ||
+ | * Javascript reads the HTML code as an object called ' | ||
+ | * In object-oriented programming, | ||
+ | * < | ||
+ | * Since we're modifying the ' | ||
+ | * Notice that the Javascript naming convention is different from HTML. HTML uses all lower case names and joins words with a < | ||
+ | * Finally, we want to assign the value ' | ||
+ | * The word ' | ||
+ | * Also notice that the line ends with a < | ||
+ | * Run this to see that it works. | ||
+ | * When satisfied, let's also add the code to restore the background to white at the end of the code. Can you do that for yourself? | ||
+ | |||
+ | ==== Prompt ==== | ||
+ | * We need a way to get input from the user. To keep this simple, we don't want to use a form at this stage. We'll use a ' | ||
+ | * Add the following statement below the second comment. | ||
+ | < | ||
+ | * This again follows the algebra pattern. Here we're assigning the user input to a variable called < | ||
+ | * We use < | ||
+ | * We write the message we want displayed inside the parentheses. In this case, it's "Give me an animal." | ||
+ | * Let's add a few more statements - one each for a name, a body part, a noun, a place and a gerund (a verb ending in ' | ||
+ | * Here's my version. | ||
+ | < | ||
+ | name = prompt(" | ||
+ | bodypart = prompt(" | ||
+ | noun = prompt(" | ||
+ | place = prompt(" | ||
+ | action = prompt(" | ||
+ | |||
+ | ==== Editing the document text ==== | ||
+ | * Now we need to add the words provided by the user to text itself. | ||
+ | * The order doesn' | ||
+ | * Add the following statement below the third comment. | ||
+ | < | ||
+ | * Again, we start with < | ||
+ | * We need a way of referring to the word ' | ||
+ | * < | ||
+ | * Clearly, we want the < | ||
+ | * It's the second < | ||
+ | * We want to replace all HTML code inside this < | ||
+ | * Finally, we give it the value we obtained from the user in the second step. < | ||
+ | * Run the code as is to check that it replaces ' | ||
+ | * Not every word we want to replace has its own tag, so lets add < | ||
+ | < | ||
+ | My friend Pablo has a <em style=" | ||
</p> | </p> | ||
+ | <p style=" | ||
+ | On Sundays they drive to the < | ||
+ | </ | ||
+ | * Now let's change all the words. The order of these statements doesn' | ||
+ | < | ||
+ | document.getElementsByTagName(' | ||
+ | document.getElementsByTagName(' | ||
+ | document.getElementsByTagName(' | ||
+ | document.getElementsByTagName(' | ||
+ | document.getElementsByTagName(' | ||
+ | * Remember to use the variable names you chose if you used different names to the ones I've used. | ||
+ | |||
+ | ==== Final Code ==== | ||
+ | * The final Javascript code will now look like this. | ||
+ | < | ||
+ | document.body.style.backgroundColor = " | ||
+ | |||
+ | // collect input from user | ||
+ | animal = prompt(" | ||
+ | name = prompt(" | ||
+ | bodypart = prompt(" | ||
+ | noun = prompt(" | ||
+ | place = prompt(" | ||
+ | action = prompt(" | ||
+ | |||
+ | // show input in story | ||
+ | document.getElementsByTagName(' | ||
+ | document.getElementsByTagName(' | ||
+ | document.getElementsByTagName(' | ||
+ | document.getElementsByTagName(' | ||
+ | document.getElementsByTagName(' | ||
+ | document.getElementsByTagName(' | ||
- | === Headline === | + | // make background white to show text again |
+ | document.body.style.backgroundColor | ||
+ | [[en: |