SLIDES: Introduction to Javascript

Download the finished Javascript Messages demo from class

Where and How to add Javascript to web pages

Link a JavaScript file to a web page using the <script> tag and the src property:

<script src="scripts.js"></script>

Insert JavaScript directly into a web page inside

<script>
	alert("Hello there.");
</script>

Never link to a file and include JavaScript inside the same

Strikethrough uses two tildes. Scratch this.

Do NOT do this:

<script src="file.js">
   alert("Hello there.");
</script>

The Javascript Console

All popular browsers have a JavaScript console, which lists any errors that the browser’s JavaScript interpreter encounters. Use this for identifying and fixing errors.

Chrome Console Keyboard Shortcuts:

Windows: Ctrl + Shift + J
Mac: Cmd + Option + J

It can be helpful to print messages to the console - for this use console.log();
You put a message inside the parentheses and that message is spit out into the javascript console in the browser.

Outputting Messages to the Page

Writing messages as alerts in a pop-up window in the browser:

alert("this is an alert message!");

Writing messages to the web page:

document.write("<h1>This is a message written to the html page!</h1>");

Writing messages to the javascript console:

console.log("this is a message to the console!");

Variables

A variable is a way of storing and keeping track of information in a program.