Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.

Similar presentations


Presentation on theme: "CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square."— Presentation transcript:

1 CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square

2 CSD 340 (Blum)2 Homage to the Square This exercise is based on the series of works by Josef Albers entitled “Homage to the Square”

3 CSD 340 (Blum)3 Use Notepad to enter the HTML code to make a red square using the tag.

4 CSD 340 (Blum)4 Code Homage to "Homage to the Square" <div style="position: relative; width: 500px; height: 500px; background: #FF0000" ms_positioning="GridLayout">

5 CSD 340 (Blum)5 Save it and open it in a browser.

6 CSD 340 (Blum)6 Add another tag within the previous.

7 CSD 340 (Blum)7 Homage to "Homage to the Square" <div style="position: relative; width: 500px; height: 500px; background: #FF0000" ms_positioning="GridLayout"> <div style="position: absolute; width: 400px; height: 400px; left: 50px; top: 75px; background: #AA5500" ms_positioning="GridLayout">

8 CSD 340 (Blum)8 Viewed in browser

9 CSD 340 (Blum)9 Add another (copy and paste comes in handy).

10 CSD 340 (Blum)10 Viewed in browser.

11 CSD 340 (Blum)11 Add another (copy and paste comes in handy).

12 CSD 340 (Blum)12 Viewed in browser.

13 CSD 340 (Blum)13 Making it interactive Now let us make a version which involves the user by letting him or her select the colors by entering RGB values. An important concept when interfacing with a user is validation – making sure the user has done something sensible within the context of the program. But we have enough to worry about with this exercise, so we will put off validation for another time.

14 CSD 340 (Blum)14 Add script tags at the bottom but before the (close-body tag).

15 CSD 340 (Blum)15 New Code alert("Welcome to Homage to Homage to the Square");

16 CSD 340 (Blum)16 The script tag The tag tells the browser that the text that follows is not HTML but something else. The language attribute informs the browser that the text within is written in JavaScript. The type attribute does the same. The reason for doing it twice is because of possible browser differences. Hopefully the browser will understand one of the versions.

17 CSD 340 (Blum)17 The alert function The line between the open and closing script tag – alert("Welcome to Homage to Homage to the Square."); is a predefined function that pops up a message window with the given text displayed.

18 CSD 340 (Blum)18 Viewed in browser.

19 CSD 340 (Blum)19 Some vocabulary A function is a set of code with some well defined action. In this case, we are just using a function that someone else wrote and that is known to most browsers. Our code is said to call the function (that is, ask that the code be run or executed).

20 CSD 340 (Blum)20 Some more vocabulary The function has an argument or parameter (in this case the text in the quotes, in the parentheses). An argument allows a function to adapt to different situations; in this case the message window can have different messages. The text in the quotes is called a literal string – we want the message literally to say exactly what we have in the quotes. The term string refers to a set of consecutive characters.

21 CSD 340 (Blum)21 A problem Suppose we wanted the message window to say Welcome to Homage to “Homage to the Square” that is, to include quotes. The browser would interpret the first quote we want printed as the ending quote of the literal. We can handle this by using an escape sequence – replacing the quotes we want printed by \”.

22 CSD 340 (Blum)22 Using escape sequence to display quotes.

23 CSD 340 (Blum)23 Viewed in browser.

24 CSD 340 (Blum)24 Vocabulary: Delimiter Note that the line of code (known as a statement) ends with a semicolon. The semicolon is known as a delimiter. A delimiter is used to separate one element from another. For example, the English language uses a space as a delimiter between words.

25 CSD 340 (Blum)25 Add the next three statements.

26 CSD 340 (Blum)26 User is prompted with message and sample input.

27 CSD 340 (Blum)27 User might change the sample input and then clicks OK.

28 CSD 340 (Blum)28 Here we alert the user as to the input he or she has given.

29 CSD 340 (Blum)29 Variables In the first new statement var FirstColor; we are said to be declaring a variable. If a quantity might change in the execution of the program, we declare a variable to set aside a place (in memory). This allows us to change it but also to hold the value until we have a chance to use it.

30 CSD 340 (Blum)30 The second new statement FirstColor = prompt("Enter a color in hexadecimal RGB (e.g. #FF0000 is red)","#FF0000"); is a combination of two things. –The first part is the predefined prompt function which has two arguments – the first is a message and the second is a sample input for the user. Both arguments are literal strings. The function brings back (returns) input from a user. –The second part is called an assignment statement (symbolized by the equal sign) that makes the variable on the left-hand side equal to the value obtained from the right-hand side.

31 CSD 340 (Blum)31 Concatenation The third new statement alert("You have selected " + FirstColor); uses the alert function again. But instead of a simple literal string as an argument, this time the argument is the concatenation of a literal string with a variable having a string value. –Concatenation is a fancy term for having one string follow another.

32 CSD 340 (Blum)32 Next phase: use the id attribute so we can identify the outermost square (div)

33 CSD 340 (Blum)33 Write code to change first square’s color based on user’s input.

34 CSD 340 (Blum)34 The code (note it shoul be on one line, not wrapped as shown below) document.getElementById("FirstSquare"). style.background = FirstColor;

35 CSD 340 (Blum)35 Viewed in browser (A)

36 CSD 340 (Blum)36 Viewed in browser (B)

37 CSD 340 (Blum)37 Viewed in browser (C)

38 CSD 340 (Blum)38 A comment Note that two slashes can be added to one of the previous statements //alert("You have selected " + FirstColor); This makes the line of text into a comment. A comment is not executed, it has no effect on what the browser displays. It is only seen when one views the source. –It can be used to disable code (as seen here) or to convey information to anyone who might read one’s code. This allows one to document one’s code which is very important.

39 CSD 340 (Blum)39 Give ids to the other squares.

40 CSD 340 (Blum)40 Add code that allows the user to change the second color

41 CSD 340 (Blum)41

42 CSD 340 (Blum)42

43 CSD 340 (Blum)43

44 CSD 340 (Blum)44 Add code for the third and fourth squares.

45 CSD 340 (Blum)45

46 CSD 340 (Blum)46

47 CSD 340 (Blum)47

48 CSD 340 (Blum)48 References Beginning JavaScript, Paul Wilton Albers, Werner Spies


Download ppt "CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square."

Similar presentations


Ads by Google