Sunday, April 22, 2007

JavaScript

1. Functions of JavaScript
A. Giving HTML designers a programming tool
B. Enabling put dynamic text into an HTML page
C. Enabling react to events
D. Enabling read and write HTML elements
E. Enabling validating data
F. Enabling detect the visitor's browser
G. Enabling create cookies

2. JavaScript Event Reference
onabort, onblur, onchange, onclick, ondbclick, onerror, onfocus, onkeydown,
onkeypress, onkeyup, onload, onmousedown, onmousemove, onmouseout, onmouseover,
onmouseup, onreset, onresize, onselect, onsubmit, onunload

** onerror event **
This event is useful to show a script error in the page.

(( Syntax ))
onerror=handleErr

function handleErr(msg,url,l)
{
// Handle the error here
return true or false
}

For using this onerror event, we need to create a function to hadle the errors.
Then we call the function with the onerror event handler. The event handler is
called with three arguments: msg(error message), url(the url of the page that
caused the error) and line(the line where the error occured).

3. How to break a code line in javascript

(( Example ))
document.write("This is \
DMT class.")

With a backslash, we can break up a code line.
But, we can't break up a code like the follwoing:

document.write \
("This is DMT class.")

4. Comments for JavaScript
A. // -- For single line comment
B. /* */ -- For multi line comment

5. Built-in JavaScript Objects

A. String Object
*Method
anchor, big, charAt, indexof, substr, toLowerCase and so on
*Property
constructor, length, prototype
B. Date Object
*Method
Date, getDate, getSeconds, getTime, setDate, toString, UTC and so on
*Property
constructor, prototype
C. Array Object
*Method
concat, join, reverse, shift, sort, toString and so on
*Property
constructor, length, prototype
D. Boolean Object
E. Math Object
*Method
abs, asin, ceil, log, random, sin, tan and so on
*Property
constructor, prototype
F. More Objects
Window, Navigator, Screen, History, Location

6. JavaScript Browser Detection

(( Example ))
var browser=navigator.appName
var b_version=navigator.appVersion
var version=parseFloat(b_Version)

document.write("Browser name:"+browser)
document.write("
")
document.write("Browser version: "+ version)

* With NAVIGATOR object, JavaScript can detect the visitor's browser type and
version.

7. JavaScript Cookie

(( Example ))
A. SetCookie
function setCookie(c_name, value, expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+"="+escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

B. GetCookie
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
.........
}
}

C. CheckCookie
function checkCookie()
{
username=getCookie('username')
if (username!=null && username!="")
{ alert('Welcome again '+username+'!')}
else
{
username=prompt('Please enter your name:',"")
if (username!=null && username!="")
{
setCookie('username',username,365)
}
}
}

* Cookies are stored on the visitor's computer. Whenever the same computer
requests a page with a browser, it will send the cookie too.
--- Cookie is usually used to identify user.

8. Creating JavaScript Objects
A. Creating instance of an object
(( Example ))
personinfoObj=new Object()
personinfoObj.firstname="John"
personinfoObj.lastname="Doe"
personinfoObj.age=50
personinfoObj.eyecolor="blue"

B. Creating template of object

function personinfo(firstname, lastname, age, eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}

StudentOne=new personinfo("Mike","Doe",30,"blue")

No comments: