Writing for the World Wide Web
JavaScript

JavaScript Basics
JavaScript is declared in your HTML document with the script tags, <script> and </script>. Within the opening script tag, you need to declare the language as "JavaScript" so the browser knows what type of code you are inserting. Below is an example script that prints the URL of your webpage on your webpage:

<script language="JavaScript">
document.write("<br />Your location: " + location.href);
</script>

Here is a basic breakdown of what each part means:

  • document—the HTML document that the script is in
  • .write—a function that tells the script to write to the document; the period tells the browser that write is an acceptable function to use on a document
  • ( )—everything that you want written to your HTML document goes between the parentheses
  • "<br />Your location: "—this is actually HTML code and regular text that will appear on the browser display, and the script will write to this information your document; whenever you have a JavaScript function that writes HTML code or regular text, the code / text must be included between quotes so the JavaScript knows this information is HTML and not JavaScript
  • location—refers to the internet location of your document
  • .href—a function that tells the script to look for the entire URL of your document; the period tells the browser that href is an acceptable function to use on a location
  •  + —the plus sign joins two different components together to be written to your HTML document; since <br />Your location: is considered one single HTML chunk, and location.href is considered one single JavaScript chunk, the plus sign shows that both of these things are separate parts, but they are both to be displayed; omitting the plus sign would make the script think <br />Your location: and location.hrefare one big chunk of information, and this would produce an error, resulting in the script not functioning at all
  • ;—the semicolon tells the script that this is the end of this line of code; if you had multiple lines of code, the semicolons would tell the browser where the end of each line of code is

The code above would produce this display:


Home | Course Information | Assignments | Schedule | Class Notes | Resources | Student Pages