To enclose JavaScript code, you use <script>
and </script>
tags.
By default, JavaScript code will output to the part of the document in which it resides. If it’s in the head it will output to the head; if the body, then the body.
You can include JavaScript code from other sources in your
documents by either copying and pasting them or, more commonly,
including them as part of a <script
src='filename.js'>
tag.
The equivalent of the echo
and print
commands used in PHP is
the JavaScript document.write
function (or method).
To create a comment in JavaScript, preface it with //
for a single-line comment or surround it
with /*
and */
for a multiline comment.
The JavaScript string concatenation operator is the +
symbol.
Within a JavaScript function, you can define a variable that has
local scope by preceding it with the var
keyword upon first assignment.
To display the URL assigned to the link with an ID of thislink
in all main browsers, you can use
the two following commands:
document.write(document.getElementById('thislink').href) document.write(thislink.href)
The commands to change to the previous page in the browser’s history array are:
history.back() history.go(-1)
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'