Javascript Handout 1.0 Javascript
Javascript Handout 1.0 Javascript
Handout 1.0
JavaScript is one of the 3 languages all web developers must learn:
1. HTML to define the content of web pages
Web pages are not the only place where JavaScript is used. Many desktop and server programs use JavaScript. Node.js is
the best known. Some databases, like MongoDB and CouchDB, also use JavaScript as their programming language.
JavaScript and Java are completely different languages, both in concept and design.
Java creates applications that run in a virtual machine or browser while JavaScript code is run on a browser only.
JavaScript was invented by Brendan Eich in 1995, and became an ECMA standard in 1997.
ECMA-262 is the official name of the standard. ECMAScript is the official name of the language.
Review:
HTML
HTML is the standard markup language for creating Web pages.
HTML tags label pieces of content such as "heading", "paragraph", "table", and so on
Browsers do not display the HTML tags, but use them to render the content of the page
HTML Elements
An HTML element usually consists of a start tag and end tag, with the content inserted in between:
The HTML element is everything from the start tag to the end tag:
HTML Attributes
Example
CSS
CSS is a language that describes the style of an HTML document.
What is CSS?
HTML was NEVER intended to contain tags for formatting a web page!
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
When tags like <font>, and color attributes were added to the HTML 3.2 specification, it started a nightmare for web
developers. Development of large websites, where fonts and color information were added to every single page,
became a long and expensive process.
To solve this problem, the World Wide Web Consortium (W3C) created CSS.
Each declaration includes a CSS property name and a value, separated by a colon.
A CSS declaration always ends with a semicolon, and declaration blocks are surrounded by curly braces.
In the following example all <p> elements will be center-aligned, with a red text color:
With an external style sheet, you can change the look of an entire website by changing just one file!
Each page must include a reference to the external style sheet file inside the <link> element. The <link> element goes
inside the <head> section:
Example
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
An external style sheet can be written in any text editor. The file should not contain any html tags. The style sheet file
must be saved with a .css extension.
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Internal Style Sheet
An internal style sheet may be used if one single page has a unique style.
Internal styles are defined within the <style> element, inside the <head> section of an HTML page:
Example
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
Inline Styles
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The style attribute can contain any CSS property.
The example below shows how to change the color and the left margin of a <h1> element:
Example
<h1 style="color:blue;margin-left:30px;">This is a heading</h1>
Introduction:
Using The class Attribute
The class attribute specifies one or more class names for an HTML element.
The class name can be used by CSS and JavaScript to perform certain tasks for elements with the specified class name.
In CSS, to select elements with a specific class, write a period (.) character, followed by the name of the class:
Example
Use CSS to style all elements with the class name "city":
<!DOCTYPE html>
<html>
<head>
<style>
.city {
background-color: tomato;
color: white;
padding: 10px;
</style>
</head>
<body>
<h2 class="city">London</h2>
<h2 class="city">Paris</h2>
<h2 class="city">Tokyo</h2>
</body>
</html>
Using The class Attribute in JavaScript
JavaScript can access elements with a specified class name by using the getElementsByClassName()method:
<!DOCTYPE html>
<html>
<body>
<p>Click the button, to hide all elements with the class name "city", with JavaScript:</p>
<h2 class="city">London</h2>
<h2 class="city">Paris</h2>
<h2 class="city">Tokyo</h2>
<script>
function myFunction() {
var x = document.getElementsByClassName("city");
x[i].style.display = "none";
</script>
</body>
</html>
Using The id Attribute
The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document).
The id value can be used by CSS and JavaScript to perform certain tasks for a unique element with the specified id value.
In CSS, to select an element with a specific id, write a hash (#) character, followed by the id of the element:
<!DOCTYPE html>
<html>
<head>
<style>
#myHeader {
background-color: lightblue;
color: black;
padding: 40px;
text-align: center;
</style>
</head>
<body>
<h2>The id Attribute</h2>
</body>
</html>
JavaScript can access an element with a specified id by using the getElementById() method:
<!DOCTYPE html>
<html>
<body>
<p>JavaScript can access an element with a specified id by using the getElementById() method:</p>
<script>
function displayResult() {
</script>
</body>
</html>