CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

31
CSD 340 (Blum) 1 Errors and Debugging and Swapping Variable Values

description

CSD 340 (Blum)3 Gives appearance of having run, but alert box never appeared.

Transcript of CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

Page 1: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 1

Errors and Debugging and Swapping Variable Values

Page 2: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 2

An HTML document with an JavaScript error.

Missing quotation mark.

Page 3: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 3

Gives appearance of having run, but alert box never appeared.

Page 4: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 4

Go to Tools/Internet Options

Page 5: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 5

On the Advanced tab, choose not to Disable script debugging and to Display a notification about every script error.

Page 6: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 6

Script error message displayed. Click on Show Details.

Page 7: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 7

Details Shown.

Page 8: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 8

Use the Edit/Go To … feature to find Line 12 where the error is claimed to be. Note errors are often found on the preceding lines, but start at line 12 and go backward in your search for an error.

Page 9: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 9

Goto Line 12

Page 10: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 10

I was taken to Line 12 which I have highlighted. The error message mentions Char(acter) 33 which is the end of the line. Again this means that the error occurs before that.

Page 11: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 11

Unterminated string constant

• The error Unterminated string constant means that we have started a string constant (or literal string – something between quotation marks) but did not finish it.

• We should be looking for a missing quotation mark.

Page 12: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 12

Another HTML document with an JavaScript error.

Misspelled “alert” – function name.

Page 13: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 13

Error message from Error2.htm

Page 14: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 14

Webopedia.com definition of runtime error

Page 15: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 15

Compile-time errors are also known as syntax errors because the are violations of the language’s rules.

Page 16: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 16

Syntax versus runtime errors.

• The statement alert("Hi there);

has a syntax error. The rules say there should be a closing quote and there is not.

• The statementalrt("Hi there");

obeys the rules. We could have defined our own function alrt as opposed to having misspelled the predefined function alert.

Page 17: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 17

Whatis.com definition of object.

Page 18: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 18

Line: 11Error: Object expected• As said earlier, the statement

alrt("Hi there");

would have been fine if alrt would have been defined. The error message is saying that it expects an object (or a function which is part of an object) to be defined to make logical sense of the statement.

Page 19: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 19

Line 11 highlighted, but real error is on Line 12. It’s saying that it expects the definition on some earlier line.

Page 20: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 20

Swap.htm viewed in browser (1)

Page 21: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 21

Swap.htm viewed in browser (2)

Page 22: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 22

Swap.htm viewed in browser (3)

Page 23: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 23

Swap.htm viewed in browser (4)

Page 24: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 24

Pre-swap code declares string variables which are image file names and uses them to them to create <img> tag code.

Page 25: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 25

The first step of the swap is have the extra swap variable take on the value of one of the other variables. This is accomplished by the statement

Swap=Image1;

If the statement were written the other way around, we would have two undefined values.

Page 26: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 26

The second step is to let Image1 (the variable whose value we placed in the Swap variable) take on the value of Image2 (the other variable). This is accomplished by

Image1=Image2;

If the statement were written the other way around, then all three variables would equal “Sun.gif” and we would have lost (irretrievably) all knowledge of “Moon.gif”

Page 27: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 27

The third and final step of the swap is to let Image2 (the second variable) take on the original value of Image1 (as opposed to the current value of Image1) – this was held in the Swap variable. This is accomplished by the statement

Image2=Swap;

If the statement were written the other way around, all three variables would have the value “Moon.gif” and we would have lost (irretrievably) all knowledge of “Sun.gif”

Page 28: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 28

Possible Mistake 1:Image1=Swap; Image1=Image2;Image2=Swap;

Page 29: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 29

Possible Mistake 2:Swap=Image1; Image2=Image1;Image2=Swap;

Page 30: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 30

Possible Mistake 3:Swap=Image1; Image1=Image2;Swap=Image2;

Page 31: CSD 340 (Blum)1 Errors and Debugging and Swapping Variable Values.

CSD 340 (Blum) 31

References

• Beginning JavaScript, Paul Wilton• http://www.webopedia.com • http://www.whatis.com