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
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
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
with JS
var regex = \^/d{4}$\;
No double quote
remember the back slash
if(!regex.test(othervar))