Wednesday, October 19, 2011

To find elements , no of elemtents .

document.write(document.anchors.length); // find all anchors on page
if yo have more than 2 forms
document.write(document.forms[0].name); // for first form name
document.write(document.images.length);// all images
document.write(document.cookie);// find cookies
 var x=document.getElementsByName("x");
  alert(x.length);// find no of elements on a page
getElementById() Accesses the first element with the specified id
getElementsByName() Accesses all elements with a specified name
getElementsByTagName() Accesses all elements with a specified tagname

javascritp style to elemnt

document.body.style.backgroundColor="lavender";

find a tag : document.getEelementByTagName

x=document.getElementsByTagName("p");

for (i=0;i<x.length;i++)
{
document.write(x[i].innerHTML);
document.write("<br />");
}

Objects in advance

objName.propName
objName.methodName()

Arrays

The following code creates an Array object called myCars:
1:
var myCars=new Array(); // regular array (add an optional integer
myCars[0]="Saab";       // argument to control array's size)
myCars[1]="Volvo";
myCars[2]="BMW";
2:
var myCars=new Array("Saab","Volvo","BMW"); // condensed array
3:
var myCars=["Saab","Volvo","BMW"]; // literal array
Note: If you specify numbers or true/false values inside the array then the variable type will be Number or Boolean, instead of String.

CREATING OBJECTS

var today = new Date()
var d1 = new Date("October 13, 1975 11:13:00")
var d2 = new Date(79,5,24)
var d3 = new Date(79,5,24,11,33,0)


If you have a function like date and want to give diffrent pararameters depending on conditions then use
can create a object with a new key word and store it in a variable and keep assignig it different parameters

JAVASCRIPT - OBJECTS

<script type="text/javascript">
var txt="Hello World!";
document.write(txt.length);
</script>

try and catch

<html>
<head>
<script type="text/javascript">
var txt="";
function message()
{
try
  {
  adddlert("Welcome guest!");
  }
catch(err)
  {
  txt="There was an error on this page.\n\n";
  txt+="Error description: " + err.description + "\n\n";
  txt+="Click OK to continue.\n\n";
  alert(txt);
  }
}
</script>
</head>

<body>
<input type="button" value="View message" onclick="message()" />
</body>

</html>

loop through object

var person={fname:"John",lname:"Doe",age:25};

for (x in person)
{
document.write(person[x] + " ");
}

while loop

<script type="text/javascript">
var i=0;
while (i<=5)
  {
  document.write("The number is " + i);
  document.write("<br />");
  i++;
  }
</script>

case statement

<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.

var d=new Date();
var theDay=d.getDay();
switch (theDay)
{
case 5:
  document.write("Finally Friday");
  break;
case 6:
  document.write("Super Saturday");
  break;
case 0:
  document.write("Sleepy Sunday");
  break;
default:
  document.write("I'm looking forward to this weekend!");
}
</script>

where to place java script

<head>
<script type="text/javascript" src="xxx.js"></script>
</head>

the if condition

variablename=(condition)?value1:value2 ;

simple and sweet


innerHTML


document.write("Hello Dolly");

above line writes any thing on page


 lets add some text to tabs;

This is when you want to add some thing in between tags

like <p></p> now if you want to add your name then use innerHTML

eg :

lets give id to the p tag so that its recognized easily "

so now it becomes : <p id="test"></p>

document.getElementById("test").innerHTML="new text added by inner HTML";

Don't be greedy : this dosent mean you can type all these in single line

document.getElementById("test").innerHTML="hello";
document.getElementById("test").innerHTML="umesh";
document.getElementById("test").innerHTML="kumar";

only last one will show;







Some basics to be brushed

"Well i am eager to play , well lets know first game and rules so that we enjoy our time "

Nothing much if you wroking on client side better look I hope these small writing can help you....