Escolar Documentos
Profissional Documentos
Cultura Documentos
Sponsored by
Upsilon Pi Epsilon
The Computer Science Honors Society
JavaScript: a brief
history
by
Developed
Netscape Communications Corporation as Mocha,
then LiveScript, and finally renamed to JavaScript.
was first introduced in Netscape version
JavaScript
2.0B3 in 1995.
Internet Explorer, JavaScript is implemented as
InJScript,
which is not exactly the same.
latest version of the language is JavaScript 1.7.
The
ECMAScript is a standardized version of JavaScript.
JavaScript: the
basics
<script>
JavaScriptcodegoeshere
</script>
part
LOOSELY TYPED - more on this later
></script>
Can src=someJSFile.js
reside in external
file also:
<script
JavaScript: the
basics
<html>
<head>
<title>thetitleofthedocument</title>
<scripttype="text/Javascript">
JavaScriptcode
</script>
</head>
<body>
HTMLCode/Tags/Contentwhatever
</body>
</html>
JavaScript: the
basics
Event Handlers
(most basic html interaction tool)
onclick, onmouseover,
onmouseout, onload,
ondoubleclick, etc.
JavaScript: Hello
World
<html>
<head>
<title>JavaScriptHelloWorld</title>
<scripttype="text/Javascript">
functiongreetings(sender){
alert(HelloWorld!);
}
</script>
</head>
<bodyonLoad="greetings();>
<h1>JavascriptHelloWorld!</h1>
</body>
</html>
JavaScript:
Challenge!
World....
When the user roles over the header, change the
text to read Hello JavaScript!.
Use an external JavaScript file.
Hint: Use Google to look up onmouseover
Hint: Event handlers can pass objects -- think of
the header as an object itself (a DOM object)
Hint: DOM Objects have an innerHTML property
Bonus: Change the font and background color
when you role over the text
Be Creative! Add whatever you want, well help.
JavaScript: Types
Number
String
Boolean
Object
Function
Array
Date
RegExp
Null
Undefined
JavaScript:
Numbers
All numbers are 64 bit floating point
(IEEE)
JavaScript: Strings
Really just Objects (like almost
everything)
JavaScript: Other
Types
Bools -- just what you think
RegExp -- Regular Expression
Objects
JavaScript:
Operators
Same as C/C++/Java: ++, +=, +, -, /, *,
Boolean expressions
== performs type coercion
0 == False .... dog == True
=== is literal comparsion
False === False .... dog !== True
If, Else, For, While, DoWhile, Switch -same
JavaScript: Objects
In JavaScript, all objects are
JavaScript: Objects
Create
varobj=newObject();
varObj{};
Add Properties
obj.name=John;
obj[name]=John;
Object Literal
Syntax
varemail{
message:HiPamela!,
details:{
to:Pamela,
from:Ross
}
}
JavaScript: Arrays
Create
vara=newArray();
a[0]=red;
a[1]=blue;
vara={red,blue};
themselves
Built-in Length property = highest
index + 1
Other Built-in methods:
a.toString(),a.toLocaleString(),
a.concat(item,..),a.join(sep),a.pop(),
a.push(item,..),a.reverse(),a.shift(),
JavaScript:
Functions
Very flexible system -- functions are all
JavaScript Objects
JavaScript:
Functions
functionadd(x,y){
vartotal=x+y;
returntotal;
}
>add()
NaN
>add(2,3)
5
JavaScript:
Functions
functionavg(){
varsum=0;
for(vari=0,j=arguments.length;i<j;i++)
{
sum+=arguments[i];
}
returnsum/arguments.length;
}
>avg(2,3,4,5)
3.5
>avg.apply(null,[2,3,4,5])
3.5
JavaScript:
Functions
You can assign functions to
JavaScript: Classes
JavaScript Classes are just functions that
functionPerson(first,last){
this.first=first;
this.last=last;
this.fullName=function(){
returnthis.first+''+this.last;
}
this.fullNameReversed=function(){
returnthis.last+','+this.first;
}
}
varross=newPerson("Ross","Boucher");
JavaScript: Classes
Previous method duplicates member
functions for every instance
JavaScript: Classes
Still another approach, using
Prototype:
functionPerson(first,last){
this.first=first;
this.last=last;
}
Person.prototype.fullName=function(){
returnthis.first+''+this.last;
}
Person.prototype.fullNameReversed=function(){
returnthis.last+','+this.first;
}
varross=newPerson(Ross,Boucher);
JavaScript:
Prototype
JavaScript: DOM
document is a built in object for
interacting with the DOM
document.getElementById(string)
allows you to get a reference to a
specific node in your document
document.createElement(tag)
document.createNode(text)
JavaScript: DOM
Documents are made up entirely of nodes
Element Nodes: every tag in your HTML is
an element
JavaScript:
Challenge 2!
it!
Create a container DIV, and a few floating divs
inside (hint: assign these inner divs to a class)
Make this look like a few boxes inside a larger box.
Add a link or form button to dynamically add new
divs inside the container (also floated)
Hint: give your container a unique ID so you can
access it with document.getElementById(myId);
Hint: use an event handler on the button
Hint: google appendChild()
Bonus: Apply a different style to added divs
Bonus++: Apply a different style every time!