git clone https://github.com/mhsu0020/CSULA-CS3220-Fall2016.git
git pull
The basics of HTML/CSS will be covered. Web design is a discipline under Art/Design. However, it doesn't hurt to understand how it works since you will be working with web/UX/UI designers in the industry. As the semester goes on, we will be leveraging several open source UI frameworks that will more or less provide some modern designs for your website.
Here are some good resources to learn web design on your own:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- text at top of brower tab -->
<title>CS3220 Rocks!</title>
</head>
<body>
<!-- page content -->
<h1>CS3220 is Awesome!!!</h1>
<p>
This class is lit
</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CS3220 Rocks!</title>
<style type="text/css">
body {
background-color: #FFC0CB;
}
</style>
</head>
<body>
<h1>CS3220 is Awesome!!!</h1>
<p>
This class is lit
</p>
</body>
</html>
<style type="text/css">
p {
background-color: #FFC0CB;
margin-left: 10%;
margin-right: 10%;
padding: 5px 5px 20px 20px;
border: 5px dotted black;
}
</style>
<a href="http://www.calstatela.edu">school site</a>
<a href="http://www.calstatela.edu" target="_blank">school site</a>
Clone the lecture repo, navigate to the examples directory under examples in lecture3, and play around with paths
<p>Each</p><p>paragraph</p><p>occupies entire blocks</p>
Each
paragraph
occupies entire blocks
<span>This </span><span>span </span><span>element is </span><span>inline so no line breaks </span><a href="#/block-inline">and links are also inline</a>
This span element is inline so no line breaks and links are also inline
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
<ol>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ol>
<img src="images/meme1.jpg" alt="memes">
/* changes font color of entire body */
body {
color: purple;
}
/* combining multiple selectors with commas */
h1, h2 {
color: red;
}
/*CSS is parsed from top to bottom,
each rules adds on/overrides the previous style*/
h1 {
color: blue;
background-color: green;
}
<ol id="food-list">
<li id="ham" class="food-item">ham</li>
<li class="food-item">pizza</li>
<li class="food-item">burrito</li>
</ol>
#food-list {
list-style-type: square;
font-size: xx-large;
}
.food-item {
color: blue;
}
#ham {
color: red;
}
External style sheets are more maintainable during development
For the labs then on, please use external style sheets as much as possible.
<head>
...
<link type="text/css" rel="stylesheet" href="css/style.css">
...
</head>
least maintainable, not recommended
<body>
...
<p style="width:400px; height:200px">
...
</p>
</body>
Order of Precedence (Applied In Order):
margin border padding element The light red shows parts of the layout. |
element This is what you see in your browser. |
<h3>Food</h3>
<div id="food-block">
<p>Food 1</p>
<p>Food 2t</p>
<p>Food 3</p>
<p>Food 4</p>
<p>Food 5</p>
</div>
The position proeprty can have one of the following four values:
In this class, we will be using Google Chrome for class demoes/project testing
Brief History:
A description of a language implementing the specs. Current version is 6. List of Components:
Note: In both cases, place the <script> tags right before the </body> tag in your document
Embedded:
<script type="text/javascript">
function showPopup(){
alert("popups are annoying");
}
</script>
External:
<script type="text/javascript" soruce="js/main.js"></script>
var myVariable = "hahaha";
//not recommended
myVariable = 2;
//not recommended
myVariable = [1, 2, 3, 4, 5];
//not recommended
myVariable = false;
Unlike Java, JavaScript is function scoped. Blocks do not have scope.
function testExample(num){
for(var i = 0; i < 10; i += 1){
}
/*Actually prints out
i with changed value*/
alert("What is 'i'? " + i);
}
Common Primitive Types:
true
and false
.42
or 3.14159
.Reference Types:
Object. Functions, Arrays, etc are all Object types
Variables can be intentionally set to null
to empty it.
Number to Strings:
In expressions involving numeric and string values with the + operator, JavaScript converts numeric values to strings.
In statements involving other operators, JavaScript does not convert numeric values to strings.
x = "The answer is " + 42 // "The answer is 42"
y = 42 + " is the answer" // "42 is the answer"
"37" - 7 // 30
"37" + 7 // "377"
String to Numbers: use parseInt()
or parseFloat
Two types of comparisons:
===
==
var obj = new Object();
var obj = {};
Given the following Objcet:
var pizza = {
name: "MyPizza",
toppings: [{name: "greenPepper", price: 40}, {name: "cheese", price: 20}]
};
var pizzaName = pizza.name;
var nameProperty = "name";
var pizzaName = pizza[nameProperty];
var firstToppingName = pizza.toppings[0]["name"];
var toppings = ["greenPepper", "cheese", "salami"];
toppings.length; //3
toppings[100] = "spam";
toppings.length; //101
Looping through arrays:
var toppings = ["greenPepper", "cheese", "salami"];
toppings.forEach(function(currentTopping, index)){
console.log(currentTopping);
}
Method name | Description |
---|---|
a.toString() |
Returns a string with the toString() of each element separated by commas. |
a.concat(item1[, item2[, ...[, itemN]]]) |
Returns a new array with the items added on to it. |
a.join(sep) |
Converts the array to a string — with values delimited by the sep param |
a.pop() |
Removes and returns the last item. |
a.push(item1, ..., itemN) |
Adds one or more items to the end. |
a.reverse() |
Reverses the array. |
a.shift() |
Removes and returns the first item. |
a.slice(start[, end]) |
Returns a sub-array. |
a.sort([cmpfn]) |
Takes an optional comparison function. |
a.splice(start, delcount[, item1[, ...[, itemN]]]) |
Lets you modify an array by deleting a section and replacing it with more items. |
a.unshift(item1[, item2[, ...[, itemN]]]) |
Prepends items to the start of the array. |
Functions are first class citizens in JavaScript: They are objects, and can be stored in variables and passed around
function add(x, y){
var total = x + y;
return total;
}
//Function Expressions
var add2 = function(x, y){
var total = x + y;
return total;
}
var addThenMutiply = function(addFunction, x, y, z){
var total = addFunction(x, y);
return total * z;
}
add(1, 2); //3
addThenMultiply(add2, 1, 2, 3); //9
function reassignPrimitive(num){
num = 2;
}
function reassignCarObject(car){
car = {make: "Ferrari"};
}
function modifyCarObject(car){
car.make = "Ferrari";
}
var number = 1;
var camry = {make: "Toyota"};
reassignPrimitive(number); //number is still 1, a copy of the value of number is passed in
reassignCarObject(camry); //camry.make is still "Toyota", a copy of the reference is passed in
modifyCarObject(camry); //camry.make is now "Ferrari"
//Global Scope Variables
var num1 = 20,
num2 = 3,
name = "Chamahk";
// This function is defined in the global scope
function multiply() {
return num1 * num2;
}
multiply(); // Returns 60
// A nested function example
function getScore () {
var num1 = 2,
num2 = 3;
function add() {
return name + " scored " + (num1 + num2);
}
return add();
}
getScore(); // Returns "Chamahk scored 5"
This creates a local scope that wraps around your code, limiting global scope pollution
//Similar to myObject.method();
//Except in this case, myObject is a function created by the function expression: (function(){}...)
(function(){
...... your javascript code here....
})();
There are 12 Node Types, most notabally:
Document
: The root Node type. The Node document
provides many useful functions, such as document.getElementById()
Element
: Most common Node Type. For example in the previous slide image, the html, head, title, body, and p elements are all Node elementsText
: represents data literals (the "text" between the tags)Getting tag name of Node: someNode.nodeName
childNodes
property containing a NodeList
.NodeList
is an array-like object used to store an ordered list of nodes that are accessible by position, similar to Arrays.
var firstChild = someNode.childNodes[0];
var secondChild = someNode.childNodes.item(1);
var count = someNode.childNodes.length;
appendChild()
: adds to endreplaceChild()
, removeChild()
cloneNode(true)
vs shallow copy cloneNode(false)
Document
TypeUseful Methods:
Getting element by ID: document.getElementById()
<div id="myDiv">Some Div</div>
.....
var div = document.getElementById("myDiv");
Getting elements by tag name: document.getElementsByTagName()
. This returns a NodeList
HTMLElement
AttributesHTMLElement
inherits from Node
with some special propertiesid
— A unique identifi er for the element in the document.className
— The equivalent of the class attribute, which is used to specify CSS classes on an element
<div id="myDiv" class="ugly-color-theme" myAttribute="stuff" >Some Div</div>
.....
var div = document.getElementById("myDiv");
div.id = "someOtherId";
div.className = "cool-color-theme";
var customProp = div.getAttribute("myAttribute");
div.setAttribute("myAttribute", "no more stuff");
The document.createElement()
function
//Doesn't actually add it to Document yet
var div = document.createElement("div");
div.id = "myNewDiv";
div.className = "box";
document.body.appendChild(div);
Directly Inserting Markup: innerHTML
Using CSS selectors, you can find elements on the page
//get the body element
var body = document.querySelector("body");
//get the element with the ID "myDiv"
var myDiv = document.querySelector("#myDiv");
//get first element with a class of "selected"
var selected = document.querySelector(".selected");
//get first image with class of "button"
var img = document.body.querySelector("img.button");
//get all em elements in a div (similar to getElementsByTagName("em"))
var ems = document.getElementById("myDiv").querySelectorAll("em");
//get all elements with class of "selected"
var selecteds = document.querySelectorAll(".selected");
//get all strong elements inside of p elements
var strongs = document.querySelectorAll("p strong");
var button = document.getElementById("myBtn");
var alertIdFunction = function(){
alert(this.id);
};
var alertHaFunction = function(){
alert("hahah");
};
button.addEventListener("click", alertIdFunction, false);
//Second click function, both will be fired when button is clicked
button.addEventListener("click",alertHaFunction, false);
btn.removeEventListener(“click”, alertIdFunction, false);
Making sure that the page is fully loaded before you access the DOM
document.addEventListener('DOMContentLoaded', function() {
...Your DOM access code here (document.getElementById)....
}, false);