Unit-3 Javascript (3)
Unit-3 Javascript (3)
Side Scripting
Chapter 6
WHAT IS JAVASCRIPT
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
What is JavaScript?
JavaScript is the Programming Language for the Web.
JavaScript can update and change both HTML and CSS.
JavaScript can calculate, manipulate and validate data.
JavaScript contains a standard library of objects, like Array, Date,
and Math, and a core set of language elements like operators
, control structures, and statements.
Syntax of Javascript:
<script>
// JavaScript Code
</script>
All kinds of scripts can run on the client side if they are written in
JavaScript, because JavaScript is universally supported.
In web development, 'client side' refers to everything in a web
application that is displayed or takes place on the client (end user
device).
This includes what the user sees, such as text, images, and the rest
of the UI, along with any actions that an application performs within
the user's browser.
It refers to the client machine (i.e., the browser) running code locally
rather than relying on the server to execute code and return the
result.
There are many client-side languages that have come into use over
the past decade including Flash, VBScript, Java, and JavaScript.
Lets focus on JavaScript due to its browser interoperability (that is, its
ability to work/operate on most browsers).
Web Development
Web Applications
Server Applications
Games
Smart Watches
Art
Machine Learning
Mobile Applications
SYNTAX
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
JavaScript Syntax
JavaScript’s reputation for being quirky not only stems from its strange way
of implementing object-oriented principles but also from some odd
syntactic gotchas:
• Everything is type sensitive, including function, class, and variable
names.
• The scope of variables in blocks is not supported. This means variables
declared inside a loop may be accessible outside of the loop, counter to
what one would expect.
• There is a === operator, which tests not only for equality but type
equivalence.
• Null and undefined are two distinctly different states for a variable.
• Semicolons are not required, but are permitted (and encouraged).
• There is no integer type, only number, which means floating-point
rounding errors are prevalent even with values intended to be integers.
(x===9) is true
<= , >= Less than or equal, greater than or equal (x<=9) is true
(x!==9) is false
Note: In javascript, ‘===‘ is a strict equality operator which checks whether its two
operands are equal in terms of both value and data type. It considers operands of
different types to be different.
JAVASCRIPT OBJECTS
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
JavaScript Objects
Objects not Classes
The p1 object is created using the object literal syntax (a short form
of creating objects) with a property named name.
items.pop(); items.push(4);
console.log(items); console.log(items);
// 1, 2, 3 // [1, 2, 3, 4]
items.shift();
console.log(items);
// [2, 3, 4]
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
JavaScript unshift() Method: var items = [1, 2, 3, 4];
JavaScript's unshift() method works similarly to
push(), however instead of adding an item to the items.unshift(0);
end of an array, a new item is prepended to the
beginning of the array: console.log(items);
// [0, 1, 2, 3, 4]
The String class has already been used without us even knowing
it.
Constructor usage
var greet = new String("Good"); // long form constructor
var greet = "Good"; // shortcut constructor
Length of a string
alert (greet.length); // will display “4"
The Date class is yet another helpful included object you should
be aware of. It allows you to quickly calculate the current date
or create date objects for particular dates. To display today’s
date as a string, we would simply create a new object and use
the toString() method.
var d = new Date();
// This outputs Today is Mon Nov 12 2012 15:40:19 GMT-0700
alert ("Today is "+ d.toString());
The getElementById() method returns null if the element does not exist.
The getElementById() method is one of the most common methods in the HTML
DOM. It is used almost every time you want to read or edit an HTML element.
<!DOCTYPE html>
<html>
<body>
<script>
const myElement =
document.getElementById("demo");
myElement.style.color = "red";
</script>
</body>
</html>
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
getElementsByTagname():
Note:
getElementsByTagName("*") returns all elements in the
document.
<script>
const collection = document.getElementsByTagName("li");
document.getElementById("demo").innerHTML = collection[1].innerHTML;
</script>
</body>
</html>
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
Element node Object
Property Description
className The current value for the class attribute of this HTML
element.
innerHTML Represents all the things inside of the tags. This can be read
or written to and is the primary way which we update
particular div's using JS.
<script>
document.getElementById("p2").style.color = "blue";
document.getElementById("p2").style.fontFamily = "Arial";
document.getElementById("p2").style.fontSize = "larger";
</script>
</body>
</html>
src Links to an external URL that should be loaded into the img,
page (as opposed to href which is a link to follow when input,
clicked) iframe,
script
value The value is related tot he value attribute of input tags. Input,
Often the value of an input field is user defined, and we textarea,
use value to get that user input. submit
JAVASCRIPT EVENTS
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
JavaScript Events
<p id="demo"></p>
</body>
</html>
function someHandler(e) {
// e is the event that triggered this handler.
}
onkeyup The user releases a key that was down (this happens last)
onchange Some <input>, <textarea> or <select> field had their value change. This could
mean the user typed something, or selected a new choice.
onfocus Complementing the onblur event, this is triggered when an element gets focus
(the user clicks in the field or tabs to it)
onreset HTML forms have the ability to be reset. This event is triggered when that
happens.
onselect When the users selects some text. This is often used to try and prevent
copy/paste.
onsubmit When the form is submitted this event is triggered. We can do some pre-
validation when the user submits the form in JavaScript before sending the data
on to the server.
FORMS
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
Validating Forms
You mean pre-validating right?
number.html empty_field.html
Password.html
If you need to get a date from the user, use the HTML5
<input type="date”>