Chapter 14 Answers

Question 14-1

To enclose JavaScript code, you use <script> and </script> tags.

Question 14-2

By default, JavaScript code will output to the part of the document in which it resides. If the head it will output to the head; if the body then the body.

Question 14-3

You can include JavaScript code from other source in your documents by either copying and pasting them or, more commonly, including them as part of a <script src='filename.js'> tag.

Question 14-4

The equivalent of the echo and print commands used in PHP is the JavaScript document.write function (or method).

Question 14-5

To create a comment in JavaScript, preface it with // for a single-line comment or surround it with /* and */ for a multiline comment.

Question 14-6

The JavaScript string concatenation operator is the + symbol.

Question 14-7

Within a JavaScript function, you can define a variable that has local scope by preceding it with the var keyword upon first assignment.

Question 14-8

To display the URL assigned to the link ID thislink in all main browsers, you can use the two following commands:

document.write(document.getElementById('thislink').href)
document.write(thislink.href)
Question 14-9

The commands to change to the previous page in the browser’s history array are:

history.back()
history.go(-1)
Question 14-10

To replace the current document with the main page at the oreilly.com website, you could use the following command:

document.location.href = 'http://oreilly.com'

Get Learning PHP, MySQL, and JavaScript now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.