tahamajs/IE / Slides /extracted_text /08-JavaScript.txt
tahamajs's picture
download
raw
11.8 kB
--- PAGE 1 ---
Introduction to
JavaScript LanguageEhsan Khamespanah
Spring 2026
1
--- PAGE 2 ---
Dynamic Behavior for Browsers
•HTML is used to specify the static structure of a web
site
•There is a need for a programming language for the
browser side
•It should be able to change HTML tags of the web
site
•It also should be able to send request to web
servers
--- PAGE 3 ---
JavaScript Language
•“The” language of the web browsers
•Widely used
•Various implementations freely available
•Useful for both web and non-web applications
•Complementing technologies: jQuery, Node.js,
CouchDB
--- PAGE 4 ---
JavaScript Nature
•It’s dynamic
•It’s dynamically typed
•It’s functional and object-based
•It fails silently
•It’s deployed as source code
•It’s part of the web platform
--- PAGE 5 ---
[No text found on this page]
--- PAGE 6 ---
Influences
--- PAGE 7 ---
History
•1995, Netscape, and Brendan Eich
•Decision on a scripting language with
Java-like syntax
•Mocha → LiveScript → JavaScript
•Standardization by Ecma Intl.
•ECMAScript specification
--- PAGE 8 ---
Using JavaScript
•In-browser scripts using <script> tag
•Embedded scripting language (e.g., Chrome
extensions, CouchDB queries, OpenOffice.org
scripts)
•Using scripting engines (e.g., QtScript, javax.script,
etc.)
•Using application platforms (e.g., ActionScript,
Ubuntu Touch, etc.)
•Using Node.js runtime engine (mainly used for
server-side programming)
--- PAGE 9 ---
Syntax
•Very similar to the widely used programming
languages
var x; // declaring a variable
x= 3 + y; // assigning a value to the variable 'x'
foo(x, y); // calling function 'foo' with parameters 'x' and 'y'
obj.bar(3); // calling method 'bar' of object 'obj'
// A conditional statement
if (x===O){ // Is 'x' equal to zero?
x = 123;
}
// Defining function 'baz' with parameters 'a' and "b'
function baz(a, b) {
return a + b;
}
--- PAGE 10 ---
Type System
•Dynamically typed
•Implicit type conversion between most types
•Primitive types: Boolean, Number, String
•Everything else is an object (including arrays and
functions)!
Boolean true, false
Number 1735, 1.65e-4, NaN, Infinity
String ‘abc’, “abc”
Two special non-
values null, undefined
--- PAGE 11 ---
Type System
•Dynamically typed
•Implicit type conversion between most types
•Primitive types: Boolean, Number, String
•Everything else is an object (including arrays and
functions)!
Boolean true, false
Number 1735, 1.65e-4, NaN, Infinity
String ‘abc’, “abc”
Two special non-
values null, undefined
Plain Objects
{
firstName: 'Jane',
lastName: 'Doe'
}Arrays [ 'apple', 'banana', 'cherry' ]
Regular Expressions /^a+b+$/
Functions function (x) { return 2 * x; }
--- PAGE 12 ---
Object References and Comparison
--- PAGE 13 ---
Undefined and Null
--- PAGE 14 ---
Type Coercition
--- PAGE 15 ---
Equality Operators: == vs ===
•JavaScript has two ways of determining whether
two values are equal:
•Strict equality (===) and strict inequality (!==)
consider only values that have the same type to
be qual
•Normal equality (==) and inequality (!=) try to
convert values of different types before
comparing them
--- PAGE 16 ---
Statements
•Variable declarations
•Assignments
•Usual control structures
•if, while, do-while, for, break, continue, switch
•Exception handling (throw, try-catch blocks)
•Function invocation
--- PAGE 17 ---
Functions
//Function Declaration
function add(x, y) {
return x + y;
}
//Function Expression
var add = function (x, y) { return x + y };
console. log(add (2, 3)); //5
//Named Function Expression
var fac = function me(n) {
if (n> 0) {
return n * me(n-1);
} else {
return 1;
}
};
console.log(fac(3)); //6
//Function Constructor
var add = new Function('x', 'y', 'return x + y');
--- PAGE 18 ---
Basic Objects
var jane = {
name: 'Jane'
describe: function () {
return 'Person named '+this.name; // (1)
}, // (2)
};
> jane.name // get property 'name'
'Jane'
> jane.describe // get property 'describe'
[Function]
> jane.unknownProperty
undefined
> jane.age = 43
--- PAGE 19 ---
Basic Objects
> var obj = { foo: 'a', bar: 'b' };
> obj.foo = undefined;
> Object. keys(obj)
[ 'foo', 'bar' ]
> delete obj.foo
true
> Object. keys(obj)
[ 'bar' ]
> var obj = { 'not an identifier': 123 };
> Object. keys(obj)
[ 'not an identifier' ]
> obj['not an identifier']
123
> var obj = { someProperty: 'abc' };
> obj['some' + 'Property']
'abc'
> var propKey = 'someProperty';
> obj[propKey]
'abc'
> var obj = { myMethod: function () { return true } };
> obj['myMethod']()
true
--- PAGE 20 ---
JavaScript HTML DOM
•Document Object Model
--- PAGE 21 ---
DOM API
<html>
<body>
<p id="p1">Hello World! </p>
<script>
document.getElementById("p1").innerHTML = "New text!";
</script>
</body>
</html>
--- PAGE 22 ---
DOM API
<html>
<body>
<p id="p1">Hello World! </p>
<script>
document.getElementById("p1").innerHTML = "New text!";
</script>
</body>
</html>
Finding
--- PAGE 23 ---
DOM API
<html>
<body>
<p id="p1">Hello World! </p>
<script>
document.getElementById("p1").innerHTML = "New text!";
</script>
</body>
</html>
Finding
Modify
--- PAGE 24 ---
DOM API
<html>
<body>
<p id="p1">Hello World! </p>
<script>
document.getElementById("p1").innerHTML = "New text!";
</script>
</body>
</html>
Finding
Modify
Add/Delete Elements
--- PAGE 25 ---
Finding Objects
document.body document.lastModified
document.URL document.title
document.forms document.referrer
document.images document.scripts
document.links document.readyState
and, there are more…
--- PAGE 26 ---
Changing Styles
<html>
<body>
<p id="p2">Hello World! </p>
<script>
document.getElementById("p2").style.color = "blue";
</script>
<p>The paragraph above was changed by a script. </p>
</body>
</html>
<htm1>
<body>
<h1 id="id1">My Heading 1</h1>
<button type="button"
onclick="document.getElementById('idl').style.color = 'red'">
Click Me!
</button>
</body>
</html>
--- PAGE 27 ---
Events Bubbling and Capturing
--- PAGE 28 ---
Events Bubbling and Capturing
document.getElementById(“myP").addEventListener(
“click", myFunction, true);
--- PAGE 29 ---
Navigating DOM
•parentNode
•childNodes[nodenumber]
•firstChild
•lastChild
•nextSibling
•previousSibling
<html>
<head>
<title>DOM Tutorial‹/title>
</head>
<body>
<h1>DOM Lesson one</h1>
<p>Hello world! </p>
</body>
</html>
--- PAGE 30 ---
Manipulating DOM
var nextId = 0;
function addInput) {
var newDiv = document.createElement('div');
newDiv.id = "choiceDiv_" + nextId;
newDiv.innerHTML = "<input type='text'..."
document.getElementById('choicesDiv').appendChild(newDiv);
nextId++;
}
var myNodelist = document.getElementsByTagName ("p");
var i;
for (i = 0; i < myNodelist.length; i++) {
myNodelist [i].style.backgroundColor = "red";
}
--- PAGE 31 ---
Browser Object Model
•window object (the top-level element)
•window.document
•window.screen
•window.location
•window.history
•window.navigator
•window.open()
•window.close()
•window.moveTo()
--- PAGE 32 ---
JavaScript Timing
<button onclick="t = setTimeout(myFunction, 3000)">
Try it
</button>
<script>
function myFunction() {
alert('Hello');
}
</script>
<button onclick="clearTimeout(t)">Stop it</button>
--- PAGE 33 ---
Back to the RIA Web Apps
--- PAGE 34 ---
Data Exchange Formats
JSON: JavaScript Object Notation
<script>
var text = '{"name": "John Johnson", "street": "Oslo West 16",
"phone": "555 1234567"}';
var obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj name + "<br>" +
obj.street + "<br>" + obj-phone;
</script>
--- PAGE 35 ---
AJAX (Asynchronous JavaScript and XML)
AJAX is a developers dream, because you can:
•Update a web page without reloading the page
•Request data from a server - after the page has
loaded
•Receive data from a server - after the page has
loaded
•Send data to a server - in the background
--- PAGE 36 ---
AJAX Example
<!DOCTYPE HTML>
<html>
<body>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest ();
xhttp.onreadystatechange = function () {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById(“demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send ();
}
</script>
<div id="demo"><h2>let AJAX change this text</h2></div>
<button type="button" onclick="loadDoc()">Change Content</button>
</body>
--- PAGE 37 ---
How AJAX Works
--- PAGE 38 ---
The AJAX Request
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
--- PAGE 39 ---
More About Ajax
•GET is simpler and faster than POST, and can be
used in most cases; however, always use POST when:
•Update a file or database on the server
•Sending a large amount of data to the server
•Sending user input (contains unknown characters)
•Synchronous Requests
xhttp.open("POST", “ajax_test.asp", true);
xhttp.setRequestHeader("Content-type",
"application/x-www-form-urlencoded" );
xhttp.send("fname=Henry&1name=Ford");
http.open("GET", "ajax_info.txt", false);
xhttp.send();
document.getElementById("demo").innerHTML =
xhttp.responseText;
--- PAGE 40 ---
onreadystatechange event
•Three important properties of the XMLHttpRequest
object:
--- PAGE 41 ---
Demo
--- PAGE 42 ---
jQuery
•jQuery is a lightweight, "write less, do more",
JavaScript library.
•Features for:
•HTML/DOM manipulation
•CSS manipulation
•HTML event methods
•Effects and animations
•AJAX
•Utilities
--- PAGE 43 ---
Using jQuery
•The jQuery library is a single JavaScript file
•If you don't want to download and host jQuery
yourself, you can include it from a CDN
•Google is an example of someone who host
jQuery
<head>
<script src="jquery-3.7.1.min.js"></script>
</head>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/
jquery.min.js"></script>
</head>
--- PAGE 44 ---
jQuery Syntax
•Basic syntax is: $(selector).action()
•A $ sign to define/access jQuery
•A (selector) to "query (or find)" HTML elements
•A jQuery action() to be performed on the
element(s)
•$(this).hide()- hides the current element
•$("p").hide()- hides all <p> elements
•$(".test").hide()- hides all elements with class="test"
•$("#test").hide()- hides the element with id="test"
--- PAGE 45 ---
jQuery Events
$("p").click(function(){
$(this).hide ();
}) ;
--- PAGE 46 ---
TypeScript
•TypeScript = JavaScript + static types
•It compiles down to JavaScript (runs everywhere JS
runs)
•Key idea: TypeScript catches errors before runtime
•Pros: fewer runtime bugs, better autocomplete,
easier refactoring
•Cons: extra syntax, learning curve, build step
required
--- PAGE 47 ---
TypeScript
•It prevents obvious bugs early
•Function Safety
•Objects and APIs
function greet(user) {
return "Hello " + user.name;
}
greet(null); // runtime error
💥
JavaScript
function greet(user: { name: string }) {
return "Hello " + user.name;
}
greet(null); //
❌ compile-time error
TypeScript
function add(a, b) {
return a + b;
}
add(2, "3"); // "23"
😬
JavaScript
function add(a:number, b:number): number
{
return a + b;
}
add(2, "3"); //
❌ error
TypeScript
type User = {
name: string;
age?: number;
};
const user: User = { name: "Alex" };
TypeScript
const user = { name: "Alex" };
console.log(user.age);
// undefined
🤷
JavaScript
--- PAGE 48 ---
JavaScript Frameworks
Vue.js

Xet Storage Details

Size:
11.8 kB
·
Xet hash:
429fcbe1b5f049c20eb85a658abdbd20c971ce4144932f0e8097f32183d85621

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.