Javascript - An
Javascript - An
Javascript - An
INTRODUCTION
: AGENDA
History
The Three Layers of the Web
Programming with JavaScript
Document Access
Browser Access
Events
Animation
Forms
Go Pro
Errors and Debugging
Ajax
CoffeeScript
Knockout.js
: HISTORY
Created by Brendan Eich in 1995 for Netscape
Navigator 2 release. Called Mocha and later
LiveScript.
On release of 1.1 name was changed to
JavaScript.
In 1997 JavaScript 1.1 was submitted to ECMA as a
proposal. TC39 was assigned to standardize the syntax
and semantics of the language.
TC39 released ECMA-262 known as
ECMAScript.
ECMAScript is basis for all the JavaScript
implementations.
: THE THREE LAYERS OF THE WEB
HTML for Content
<p class=“warning”>There is no <em>download
link</em> on this page.</p>
Prototype
Script.aculo.us
Dojo
jQuery
MooTools
Knockout
: PROGRAMMING WITH JAVASCRIPT
Running a JavaScript program/script.
There are two ways:
There was once a
language attribute
<script type=“text/javascript”> also in the script tag.
window.alert(“JavaScript”); i.e.,
language=“javascript
</script> 1.5” or whatever
<script type=“text/javascript”
src=“myScript.js”></script>
<script type=“text/javascript” src=“myScript.js”
defer></script>
STATEMENTS, COMMENTS AND VARIABLES
Person.speakName();
ALTERNTAE SYNTAX
Alternate objects syntax: i.e.,
It’s JSON
(JavaScript
var Person = { Object
name: “Manvendra SK”, Notation)
syntax
age: 23,
speakName: function() { alert(“Hello,
I’m ” + this.name);
}
};
Person.speakName();
CREATING REAL OBJECTS
We can create objects those can be instantiated using
the new keyword.
var Person = function(name, age)
{ this.name = name;
this.age = age;
};
Person.prototype.speakName = function()
{ alert(“Hello, I’m ” + this.name);
};
USING REAL OBJECTS
var manvendra = new Person(“Manvendra”, 23);
manvendra.speakName();
document.getElementByTagName(“label”)
[0].style.position = “absolute”;
document.getElementByTagName(“label”)
[0].style.left = “300px”;
Event
handler
Event
listener
Event
listener
ATTACHING EVENT LISTENERS
How to attach event listener?: i.e.,
element.addEventListener(“event”,
eventListener, false);
Object detection:
typeof element.property != “undefined”
EVENT PARAMETER
Event parameter of the listener: Browser
automatically passes the event parameter to the event
listener function. i.e.,
function someClickEventListener (event) {
// Use event argument’s methods
}
event parameter has some important methods.
Some of them are: preventDefault() and
stopPropagation().
EVENT OBJECT
IE has its own way. If you remember IE doesn’t
expect third argument. It means it also doesn’t pass
event parameter to the event listener function. Then
how to access it?
In IE there is a global event object, which is
window.event. And it has equivalent properties like the
event parameter which are: returnValue which we can
set to false, and cancelBuble which we can set to true.
These two settings achieves the same effect as its event
parameter counterpart’s methods.
DETACHING EVENT LISTENERS
How to detach event listener: i.e.,
element.removeEventListener(“event”,
eventListener, false);
For IE,
var requester = new
ActiveXObject(“Microsoft.XMLHTTP”);
PROPER INSTANTIATION
Example of instantiating the XMLHttpRequest: try {
var requester = new XMLHttpRequest();
} catch (error)
{ try {
var requester = new
ActiveXObject(“Microsoft.XMLHTTP”);
} catch (error) {
var requester = null;
}
}
USING THE XHR
Using the XMLHttpRequest:
requester.setRequestHeader(“Content-Type”,
“application/x-www-form-urlencoded”);
// Optional
requester.open(“GET”, “/url.xml”, true);
requester.send(null);
requester.onreadystatechange = function() {
// Use requester.readyState property, which is 0 to 4,
uninitialized, loading, loaded, interactive, complete.
// Also now check requester.state property, which
contains HTTP codes of response. For success use 200
or 304, other are failures.
READ THE DATA FROM XHR
Where is the data:
responseXML: If the server responsed with content- type set
to text/xml then we have DOM. We can traverse it as usual
to get the data. It also populates the responseText property,
just for an alternative.
responseText: If the server responsed with the content-type
set to text/plain or text/html then we have single string as
the data. But now the responseXML will be empty.
: COFFEESCRIPT
CoffeeScript is a new way to write tricky and
lengthy JavaScript easily and intuitively.
http://coffeescript.org/
: KNOCKOUT.JS
Knockout is a MVVM (Model-View-ViewModel)
library. MVVM is an extension of MVC, originally
created by Microsoft. Knockout allows us to do UI
bindings declaratively rather than using JS.
http://knockoutjs.com/
: EXERCISES
Prompt for amount, interest rate and number of years.
Now calculate the Simple Interest using the values,
and show in alert box.
Check if any given string is palindrome. Use
input element to get the string.
Calculate the area of circle.
On click of input button ask the user name and
display it inside a input text box.
Copy text of first text box to second text box on
every change of value in first text box.
…Continued
Allow submission of form only if the user has
entered his name (should not be blank) and age (must
be greater than or equals to 18).
Change the color of a div element on mouse over and
restore it on mouse out.
Create a Clock object and encapsulate methods like
start and stop for starting and stopping the clock.
Implementation must use Prototype pattern and event
listener mechanism. Display the clock in some div or
span or p element.