-
some stupid things
- if else java style
- for(var x=0;x<2;x++)
-
Array
- var somearray = new Array();
- var 2Darray = new Array(new Array(2),new Array(3));
-
function
-
function changeImage(xyz)
- Remember no var is required for xyz.... inside the method signature
- But xyz act as local variable
- return x;
-
var funcName = changeImage;
- No function bracket
- funcName("some stupid thing");
-
Callback
-
browser supported callback method
- e.g.,
- onload
- onclick
- window.onload = changeImage;
- Just Referencing the method.. not calling it
- How it work ??
- Onload happen ===> window.onload() ===> call changeImage()
- Adv...
- No need to put those event handling thing in the html like onclick etc
- Disadv...
- How to pass variable inside method...
- function literal
- document.getElementById("abc").onclick = function(evt){ changeImage(123);}
- also called as anonymous functions
- window.onload = function(){ ... }
- In this method other function literal can be called
- So When ever HTML page get unloaded, you have to initialize all the function literal methods.
-
Browser
-
cookie
- document.cookie
- navigator.cookieEnabled
-
Form and Validation
-
Accessing element
- getElementById()
-
form object
-
Step 1: onclick="showIt(this.form)"
- Instead of sending this.form... only this can be passed...
- Step 2: function showIt(theForm){ theForm["zipcode"].value };
- form.submit();
-
When to validate??
- Select the input field... onfocus
-
Leave the input field and move to the next one... onblur/onchange
- onchange only diffrence is ... contents needs to be changed
- onchange should not be used...bcoz it won't get trigger for empty element.
-
Regular Expression
-
Pattern
-
pattern = ###
- all numeric
-
pattern = /^\d{5}$/
- All regular expr.. enclosed by /
- d ==> a single numeric digit
- {5} ==> The single digit must repeat 5 times
- $ ==> The string must end with this pattern
-
char overview
-
.
- Match any char other than new line
-
*
- 0 or more times
-
+
- 1 or more times
-
?
- 0 or 1 times
-
{n}
- repeat n times
- e.g., /\d{4}/
- Digit should be repeated 4 times
- e.g., 3000
-
()
- Group something
-
\s
- Match a whitespace char
-
\d
- match any numeric digit
-
^
- The string must begin with the pattern
-
\w
- match any alpha numeric char
-
$
- The string must end with the pattern
-
{min,max}
- e.g., /^\w{5,8}$/
-
|
- or
- e.g., /red|blue/
-
characterclass
- /d[iu]g/
- "dig"
- "dug"
- /\$\d[\d\.]*/
- $1
- $5.05
- $6.
-
example
-
/^cat/
- category
- cat
- cat123
- 123cat
-
/\d\d\d/
- 007
-
/^\d\d\d\d$/
- AUS zip code
- must start with the pattern
- must end with the pattern
- 4 digit in a row
- e.g., 3000
-
/^\d{5}-\d{4}$/
- e.g., 40012-3214
-
/\w*/
- Any number of alpha numberic
- encluding empty string
-
/^\w+@\w{2,8}\.\w{2,3}$/
- email
- period is escaped . ===> \.
- PROBLEM
- abc_abc@abc.com
- abc+abc@abc.com
- abc.abc@abc.com
- abc@abc-xyz.com
- abc@abc.comb
- abc@abc.xy.zy
- New
- /^\w+@\w{2,8}\.\w{2,3}\.+\w{2,3}$/
- mine
- /^[\w_\+\.]+@[\w_]+(\.\w{2,4})+$/
-
with JS
-
var regex = \^/d{4}$\;
- No double quote
- remember the back slash
- if(!regex.test(othervar))
-
HTML and DOM
- document.getElementById("id1")
-
innerHTML
-
example
- <p id="story">some story I am going to <b>write</b>.</p>
- access the above <p> element
- innerHTML of the above will contain.... some story I am going to write.
- No <b></b> tag
- but write ll be bold in color
-
node
-
nodeValue
- only for text and attribute nodes
- not for element
-
example
- <p id="id1">abc</p>
- document.getElementById("id1").firstChild.nodeValue
-
nodeType
- returned as integer
- childNodes
- firstChild
- lastChild
-
Modifying contents
-
simple way
-
document.getElementById("id1").firstChild.nodeValue = "some stupid text";
- using this the p value can be accessed as well as modifed
- you cannot clear or assign other child elements by using this method..
-
improved way
-
Remove all child nodes
- removeChild()
-
Create a new text node based upon the new content.
- createTextNode()
-
Append the newly created text node as child node
- appendChild()
-
DOM vs innerHTML
- DOM is standard
- DOM more control
- DOM difficult to learn also :)
- innerHTML is not. may have browser compability issue.
- replaceNodeText
-
class
- <p id="id1" class="dog">doggy</p>
- document.getElementById("id1").className = "cat";
-
style properties
- document.getElementById("id1").style.visibility = "hidden";
- "visible"
-
creating any HTML element
- document.createElement("p");
- document.createTextNode("hello wold");
- but this floating p element.. we shud place in proper place
-
Objects
-
data + action = object
- objects link variable and functions together inside a storage container
-
object . property/method
-
example
- example.myname = "Hello";
-
example.killMe();
- No need to pass any variable
-
new operator
- var example = new Example("Hello", "something");
-
constructor
- example
- consturctor first letter always capitalize
- no need to create the variable separately
-
Date
- var now = new Date();
-
var somDate = new Date("MM/DD/YYYY");
- alert(someDate);
- alert(someDate.toString());
- time in milli seconds
-
methods
- setMonth();
- setYear();
- getDate();
-
getDay();
- 1 to 31
-
getFullYear();
- 2008
-
getMonth();
- 0 to 11
-
getDaysBetween
-
Tested
- DD/MM/YYYY
- MM/DD/YYYY
-
sort
-
Standard
- var someArray = [5,1,2,6];
-
someArray.sort();
- [1,2,5,6]
-
custorm
-
compare(x,y)
- function compare(x,y){return x-y;}
- ascending order
- <0
- 1 - 2 = -1 , 1 ahead of 2
- =0
- 1 -1 = 0, no need to do anything
- >0
- 2 - 1, 1 ahead of 2 reverse
- wnt to make desc???
- return y - x;
- someArray.sort(compare);
- function literal
- someArray.sort(function(x,y){return y-x;});
-
String
- length
- indexOf()
- charAt()
- toLowerCase()
- toUpperCase()
- Can be used for searching !!! do by ur own :)
-
Math
- PI
- round()
- floor()
- ceiling()
-
random()
- 0 to 1
- e.g., 0.11853043938005059
- 1 to 6
-
Custom Objects
- class and instance concept
-
prototype
- hidden property
- set properties and methods that are owned at class level
- e.g.,
-
class property
- This class property is accessible to all instance
- also I can add my method in standard object such as String
-
class level method
- Blog.myMethod = function() { ... }
- call like any static method... Blog.myMethod from outside
-
debugg
-
error type
- logic error
- runtime error
- syntax error
-
debugg method
-
alert
- suggested by Head First
-
AJAX
-
XMLHttpRequest
-
function
- send()
- open()
-
abort()
- cancel the request
-
properties
-
readyState
- 0
- uninitialize
- After request object created...
- 1
- open
- After the open statement called
- 2
- sent
- Server responds to the browser while it is working on the request...
- 3
- receiving
- Data is downloading but not ready to be used
- 4
- loaded
- Response arrived from the server
- Funda!!!
- Browser actually tells the callback method where the request is present in the above lifecycle.
-
status
- HTTP status
- 404 not found
- 200 OK
-
onreadystatechange
- A reference to the functioin which is to be called when the state of the request changed
- Browser call 4 times from 1 to 4 the assigned call back method.....
- It calls each time readyState got changed
-
responseText
- string or plain text
- responseXML
-
STEPS of one simple request
- create this object Browser Compatibility
-
open the request
- request.open(type, url, true);
- opening the request it is ready to be sent to server
- type
- GET
- POST
- true
- Asynchronous
- this script run in backgroung without making the scrip to wait.
- request.open("GET","some.do?name=praful",true);
- Remember to escape "praful" before sending.. if u r using variable... Otherwise it may contain & etc
-
Set request handler functioin
- request.onreadystatechange = handler;
- remember not to put ()
- the above "handler" method will be called when the server respond to the request.
-
request.send(null)
- request sent with no data
-
Server Code....
- response.getWriter().print("return anything u wnt");
- Handle Response
-
example
-
POST with sending some param
- request.setRequestHeader is must as the data is passed to server.
-
Gyan
-
separate
- Page's content and structure (XHTML)
-
Behavior (JavaScript)
-
window.onload = initPage;
- () NOT REQUIRED
- inside initPage() ==> all event handling should go here
- Presentation (CSS)
-
reuse
- put the application page independent code in utils class
- utils.js
- put utils.js before validation.js