GDI Intro to JavaScript Class Survey Survey Link
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Tell us about yourself.
You will need a text editor and a browser.
I mean it is JAVAscript!
Photo Credit: Josh Sager
Despite the constant changes programming fundamentals ALWAYS STAY THE SAME!
Nearly all programming languages have the same core fundamentals. Which are the following:
If you can master these 6 concepts, then you will have the ability to work with nearly every programming language you come in contact with.
An added benefit of learning how to write JavaScript is that it's C style syntax is very similar to many other popular languages.
This is Tiobe list of the currently most popular programming languages.
At the time these slides were authored there are only 33 reserved words. Which is the core part of the language. I find that encouraging!
Most everyday development only requires less than half of them.
It is a just in time complied language.
Photo credits: Andrew E. Larson and John Seb Barber cc
Let's write your first JavaScript program. Make a folder called gdi. Inside, make a new page called index.html. Place this code inside.
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<p>This is my awesome JavaScript code.</p>
<script>
alert('Hello World!');
</script>
</body>
</html>
You can mix JavaScript and HTML. The script tag tells your browser the stuff inside is code, not content.
<script>
CODE GOES HERE
</script>
Just like CSS, you can split a long block of JavaScript into its own file.
<script src="path/to/file.js"></script>
After each individual statement, you must add a semicolon.
<script>
console.log('Hello World!');
console.log('I am glad to meet you');
console.log('I am fuzzy');
</script>
console.log(); is a special command that can only be seen by the developer tools.
The console is a GREAT debugging tool. Most modern browsers have a console.
You can leave comments in your code—notes that people can read and computers will ignore.
<script>
/*I can wrap long comments
with multiple lines
like this*/
console.log('Hello World!'); //Or mark short comments like this
</script>
Open a popup box.
alert('Hello World!');
Display a message in your console.
console.log('Hello World!');
Add something to the page.
document.write('Hello World!');
When people talk about javascript. Usually they are talking about 2 things.
It's a tree!
It's the nested structure of an HTML page in the form of a tree. The browser makes a "map" of all the elements on a page.
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
<style>
h1 {
color: red;
}
</style>
</head>
<body>
<h1>My Page</h1>
<p>Hello World!</p>
<img src="http://placebear.com/200/300" alt="bear"/>
</body>
</html>
A website is a way to present your content to the world, using HTML and CSS to present that content & make it look good.
The "#" is how you tell CSS "this is an id."
The "." is how you tell CSS "this is a class name."
We are going to change the DOM with Javascript.
document.getElementsByTagName("h3")[0].style.backgroundColor="orange";
You can get certain types of HTML elements using this method:
// document.getElementsByTagName(tagName) - get list;
document.getElementsByTagName("a");
// document.getElementsByTagName(tagName)[number] - get 1;
document.getElementsByTagName("0");
In modern browsers, you can use other methods too.
Available in IE9+, FF3.6+, Chrome 17+, Safari 5+:
// document.getElementsByClassName(className);
document.getElementsByClassName("example");
Available in IE8+, FF3.6+, Chrome 17+, Safari 5+:
// document.querySelector(cssQuery) - Get first one;
document.querySelector(".example");
// document.querySelectorAll(cssQuery) - Get All;
document.querySelectorAll(".example");
Any method that starts with "getElement" will return a single node.
document.getElementById('uniqueID'); //returns a single node
Any method that starts with "getElements" will return a array of nodes. To modify a single node, you will need to use bracket notation to select the correct one.
document.getElementsByTagName('p'); //returns multiple nodes
var specficParagraph = document.getElementsByTagName('p')[2];
We are going to change EVEN MORE of the DOM with Javascript.