Lecture 6 HTML & Css
Lecture 6 HTML & Css
Shermeen Adnan
JavaScript
• From Wikipedia:
• ... high-level, dynamic, untyped, and interpreted programming
language
• ... is prototype-based with first-class functions, …
• ... supporting object-oriented, imperative, and functional
programming ...
• … has an API for working with text, arrays, dates and regular
expressions
• Not particularly similar to Java: More like C crossed with Self/Scheme
– C-like statements with everything objects, closures, garbage collection, etc.
• Also known as ECMAScript
JavaScript
• What is JavaScript?
– JavaScript, often abbreviated as JS, is a high-level,
interpreted programming language that conforms to the
ECMAScript specification.
– 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.
• Why to study JavaScript?
– HTML to define the content of web pages
– CSS to specify the layout of web pages
– JavaScript to program the behavior of web pages
JavaScript
• What can you do with it?
– Build interactive web pages
– Web/mobile apps
– Real-time networking apps (chats and video streaming services)
– Command-line tools
– Games
• Where does JavaScript code run?
– It can be run outside the browser as well
• Originally designed to run in browsers (containing JS engine)
– FireFox : SpiderMonkey
chrome : v8
• Later Open source JavaScript engine chrome was embedded into C++ program called
“node”
• What is the difference between ECMAScript and JavaScript?
– ECMAScript (or ES) is a scripting-language specification standardized by Ecma
International to standardize JavaScript, so as to foster multiple independent
implementations.
Setting up the development environment
• To install third-party libraries
– node.js //nodejs.org
– Node.js is an open-source, cross-platform JavaScript run-time
environment that executes JavaScript code outside of a browser.
• To check if it is already installed onto your computer
– In cmd prompt write
• Node –v (returning the version of node.js being installed on your machine)
• Live server
– Used to serve web applications
• ctrl+shift+p (package control : install package) repository will be loaded
browser sync
• This procedure will install package browser sync
• To launch browser sync in browsers other than chrome
• http://localhost:3000 (address from where our web application is served
from)
Setting up the development environment
• Go to "Tools > Build System > New Build System" in
the top bar
– { "cmd": ["node", "$file"], "selector": "source.js" }
– Save under USER folder with name node.sublime-build
• Go to "Tools > Build System > New Build System" in
the top bar
– { "cmd": ["C:/Program Files/nodejs/node.exe", "$file"],
"selector": "source.js" }
– Save this file as a JavaScript.sublime-build in “User”
directory
• To open JavaScript in chrome (ctrl+b)
• To open in sublime text (F7 key)
Setting up the development environment
– <script>
– function displayDate() {
– document.getElementById("demo").innerHTML = Date();
–}
– </script>
– <p id="demo"></p>
Common events
String Methods and Properties
• String Length
– The length property returns the length of a string:
• <p id="demo"></p>
• <script>
• var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
• document.getElementById("demo").innerHTML =
txt.length;
• </script>
String Methods and Properties
• Finding a String in a String
– The indexOf() method returns the index of (the position of)
the first occurrence of a specified text in a string:
• <p id="demo"></p>
• <script>
• var str = "Please locate where 'locate' occurs!";
• var pos = str.indexOf("locate");
• document.getElementById("demo").innerHTML = pos;
• </script>
– The lastIndexOf() method returns the index of the last occurrence of a
specified text in a string
– Both indexOf(), and lastIndexOf() return -1 if the text is not found.
String Methods and Properties
• Searching for a String in a String
– The search() method searches a string for a specified value and
returns the position of the match:
• <p id="demo"></p>
• <script>
• var str = "Please locate where 'locate' occurs!";
• var pos = str.search("locate");
• document.getElementById("demo").innerHTML = pos;
• </script>
• The only difference with indexOf() is that the search() method
cannot take a second start position argument.
String Methods and Properties
• Extracting String Parts
– There are 3 methods for extracting a part of a string:
• slice(start, end)
• substring(start, end)
• substr(start, length)
– The slice() Method
• slice() extracts a part of a string and returns the extracted part in a new
string.
• The method takes 2 parameters: the start position, and the end position
– <p id="demo"></p>
– <script>
– var str = "Apple, Banana, Kiwi";
– var res = str.slice(7,13);
– document.getElementById("demo").innerHTML = res;
– </script>
– <script>
– function myFunction() {
– var str = " Hello World! ";
– alert(str.trim());
–}
– </script>
Arrays
• JavaScript arrays are written with square
brackets.
• Array items are separated by commas.
– Let selectedColors=['red' , 'blue' , 'green'];
– Length of array
• console.log(selectedColors.length);
• Array indexes are zero-based, which means
the first item is [0], second is [1], and so on.
Functions
• A block of code designed to perform a particular task.
• Executed when "something" invokes it (calls it).
• A JavaScript function is defined with the function keyword,
followed by a name, followed by parentheses ().
• Function names have same rules as variables.
• The parentheses may include parameter names separated
by commas: (parameter1, parameter2, ...)
• The code to be executed, by the function, is placed inside
curly brackets: {}
– function name(parameter1, parameter2, parameter3) {
// code to be executed
}
– Function_name(argument);
Function Invocation
• The code inside the function will execute
when "something" invokes (calls) the
function:
– When an event occurs (when a user clicks a
button)
– When it is invoked (called) from JavaScript code
– Automatically (self invoked)
Function Return
• When JavaScript reaches a return statement,
the function will stop executing.
• If the function was invoked from a statement,
JavaScript will "return" to execute the code
after the invoking statement.
• Functions often compute a return value. The
return value is "returned" back to the "caller"
Types of Functions
• Performing a task
– function greet(Fname, Lname) {
– console.log('Greetings' + ' ' + Fname + ' ' + Lname);
–}
– greet('hello', 'girl');
• Calculating a value
– function square(number) {
– return number * number;
–}
– let number = square(11);
– console.log(number);
Why Functions?
• You can reuse code: Define the code once, and
use it many times.
• You can use the same code many times with
different arguments, to produce different
results.
JavaScript Arithmetic Operators
– <script>
– document.getElementById("demo").innerHTML =
"Hello JavaScript!";
– </script>
• JavaScript accepts both double and single quotes
JavaScript
• <!DOCTYPE html>
• <html>
• <body>
• <button type="button"
• onclick="document.getElementById('demo').innerHTML = Date()">
• Click me to display Date and Time.</button>
• <p id="demo"></p>
• </body>
• </html>
what JavaScript can do?
• JavaScript can change HTML content
– <!DOCTYPE html>
– <html>
– <body>
– <h1>My First JavaScript</h1>
– <script>
– function myFunction() {
– document.getElementById("demo").innerHTML = "Hello JavaScript!";
–}
– </script>
– </body>
– </html>
what JavaScript can do?
• JavaScript can change HTML styles
– <!DOCTYPE html>
– <html>
– <body>
– <h1>My First JavaScript</h1>
– <script>
– function myFunction() {
– document.getElementById("demo").style.fontSize = "25px";
– document.getElementById("demo").style.color = "red";
– document.getElementById("demo").style.backgroundColor = "yellow";
–}
– </script>
– </body>
– </html>
what JavaScript can do?
• JavaScript can change HTML attributes
– <!DOCTYPE html>
– <html>
– <body>
– <script>
– function light(sw) {
– var pic;
– if (sw == 0) {
– pic = "pic_bulboff.gif"
– } else {
– pic = "pic_bulbon.gif"
– }
– document.getElementById('myImage').src = pic;
–}
– </script>
– </body>
– </html>
The HTML <noscript> Tag