Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, November 7, 2011

How to Open New Window in Java script


<div dir="ltr" style="text-align: left;" trbidi="on">
<script type="text/javascript">
// Popup window code
function newPopup(url) {
popupWindow = window.open(
"http://www.developmentbucket.com",'popUpWindow','height=500,width=500,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,
location=no,directories=no,status=yes')
}
</script>
</div>

Saturday, November 5, 2011

How to extract email id from text in JavaScript: Java Script


Just copy and paste in HTML file......

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

function findEmailAddresses(StrObj) {
var separateEmailsBy = ", ";

var emailsArray = StrObj.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
if (emailsArray) {

for (var i = 0; i < emailsArray.length; i++) {
if (i != 0) email += separateEmailsBy;
 emailsArray[i];
document.getElementById("gg").innerHTML+='<br>'+ emailsArray[i];
      }
   }
return email;
}

</script>
</HEAD>



<BODY>

<center>
<form>
<textarea name=comments name="cc" rows=10 cols=50 onBlur="this.form.email.value=findEmailAddresses(this.value);"></textarea>
<br>
Email:  <input type=button value="Find Emails" onClick="findEmailAddresses( document.getElementsByName('cc');">
</form>
<span id="gg"></span>
</center>
</BODY>



Monday, April 18, 2011

How to Go Back Last Page in Java Script!


Windows.history.back();
Syntax
history.back()

<html>
<head>
    <title>History: Go Back</title>
    <script language="javascript">
function goback()
{
window.history.back();  //for go back
}
</script>
</head>
<body>
<p>You can go back by clicking this button </p>
<input type="button" value="Go back" onclick="goback()" />
</body>
</html>

Thursday, April 14, 2011

How to make a funtion in Java Script:



<html>
<head>
<script language="javascript">
//funtion with to paramenters
function power(x,y) //return the x to the power y
{
pow=1;
for(i=1;i<=y;i++)
pow=pow*x;
return pow; //how to return values
}
//function without paramenters
function display() /*use for getting the values from the form inputs and setting the power to the third text box */
{
var a=document.getElementById('f').value;
var b=document.getElementById('s').value;
var res=power(a,b);
document.getElementById('res').value=power(a,b);
}

</script>
</head>
<body>
<form >
<input type="text" id="f"/>
<input type="text"  id="s"/>
<input type="button" onclick="display()" value="Power" />
<input type="text" id="res" />
</form>

</body>
</html>

How to perform mathematics openration in Java Script(Math):



A built-in object that has properties and methods for mathematical constants and functions.
Find the power of a number Math.pow(num):
<script language="javascript">

document.write("<h2>How Many bits in 2 bytes?</h2>");
document.write("<h2>Ans:"+Math.pow(2,8)+"</h2");

</script>
Find the square root of a number Math.sqrt(num):
<script language="javascript">

document.write("<h2>what is square root of 200?</h2>");
document.write("<h2>Ans:"+Math.sqrt(200)+"</h2");//power function

</script>
Find out greater number form two numbers Math.max(num1,num2):
<script language="javascript">

document.write("<h2>"+Math.max(10,20)+"</h2>");/*max return the greater number */

</script>
Find out lesser number form two numbers Math.min(num1,num2):
<script language="javascript">

document.write("<h2>"+Math.min(10,20)+"</h2>");//min return the lesser number

</script>
Find out the value of PI constant:
<script language="javascript">

PI=22/7;
document.write("<h2>Value by 22/7="+PI+"</h2>");
document.write("<h2>Value by Math.PI="+Math.PI+"</h2>");
</script>
Find out the value of log base e:
<script language="javascript">

document.write("<h2>Value of log 2 base e="+Math.log(2)+"</h2>");

</script>
Find out the value of Euler constant ex:
<script language="javascript">

document.write("<h2>Value of e<sup>3</sup>="+Math.exp(2)+"</h2>");
</script>

How to display present date and time in Java Script (Date):



<script language="javascript">

timeA = new Date();
document.write(timeA.toLocaleTimeString()+"<br/>"); //to display time
document.write(timeA.toDateString()+"<br/>"); //to display date
document.write(timeA.toString()+"<br/>");//to display date and time both

</script>

How to find the Length of the Array In java script:


<script language="javascript">

Arr1=new Array(2,1,4,3) ;
document.write("<h1>Length of Arr1 is="+ Arr1.length+"</h1>");

</script>

How to add, remove, and chage content of the Array in Java Script (splice(start, delete,element1,element2,…)):


<script language="javascript">

Arr1=new Array("RED","YELLOW","GREEN","BLUE","WHITE","BLACK") ;
Arr1.splice(1,0,"PINK","ORANGE");
/*If you put 1 in place of 0 then one element will be removed from the array and if you put 0 inplace of 1
elements added from the begining of the array */
for(i=0;i<Arr1.length;i++)
{
document.write(Arr1[i]+"<br/>");
}

How to extract elements from an Array in Java Script (slice(start, end)):


<script language="javascript">

Arr1=new Array("RED","YELLOW","GREEN","BLUE","WHITE","BLACK") ;
arrslice=Arr1.slice(0,3);//extract 3 element (not include end ) and create another array arrslice
for(i=0;i<arrslice.length;i++)
{
document.write(arrslice[i]+"<br/>");
}

</script>

How to remove the first element for the Array in Java Script (shift()):


Shift():Removes the first element from an array and returns that element.

<script language="javascript">

Arr1=new Array("RED","YELLOW","GREEN","BLUE","WHITE","BLACK") ;
document.write("<h2>Element of Array Before shift()</h2><br/>");
for(i=0;i<Arr1.length;i++)
{
document.write(Arr1[i]+"<br/>");
}
Arr1.shift()//remove and return element and chage the length of the array.
document.write("<h2>Element of Array After shif()</h2><br/>");
for(i=0;i<Arr1.length;i++)
{
document.write(Arr1[i]+"<br/>");
}

</script>

Method for Array operations: concat() and sort():

 
concat() : Joins two arrays and returns a new array.
Sort() :Sorts the elements of an array.

<script language="javascript">

Arr1=new Array(2,1,4,3) ;
Arr2=new Array(6,8,9,7) ;

document.write("<h2>First Array Arr1</h2>");
for(i=0;i<Arr1.length;i++)
document.write(Arr1[i]+"<br/>");

document.write("<h2>First Array Arr2</h2>");
for(i=0;i<Arr2.length;i++)
document.write(Arr2[i]+"<br/>");

Arr3=Arr1.concat(Arr2); //user for concat two array
document.write("<h2>First Array Arr1 after concat</h2>");
for(i=0;i<Arr3.length;i++)
document.write(Arr3[i]+"<br/>");

Arr3.sort();//user for sorting
document.write("<h2>First Array Arr1 after sort()</h2>");
for(i=0;i<Arr3.length;i++)
document.write(Arr3[i]+"<br/>");


</script>

Join:
join()  The string conversions of all array elements are joined into one string.

<script language="javascript">

Arr1=new Array("RED","YELLOW","GREEN") ;
MYVAR=Arr1.join();
document.write(MYVAR);


</script>

push() and pop():
push() : Add  elements to the end of an array and returns the new length of the array. This method changes the length of the array.
pop():Removes the last element from an array.

<script language="javascript">

Arr1=new Array("RED","YELLOW","GREEN") ;
document.write("<h3>Elements of array before push()</h3>");
for(i=0;i<Arr1.length;i++)
{
document.write(Arr1[i]+"<br/>");
}

Arr1.push("BLUE","WHITE","BLACK");
document.write("<h3>Elements of array after push()</h3>");
for(i=0;i<Arr1.length;i++)
{
document.write(Arr1[i]+"<br/>");
}
document.write("<h3>Elements of array after pop() 3 times</h3>");
for(i=0;i<=2;i++)
{
Arr1.pop();
}
for(i=0;i<Arr1.length;i++)
{
document.write(Arr1[i]+"<br/>");
}

</script>
reverse(): the first array element becomes the last and the last becomes the first.

<script language="javascript">

Arr1=new Array("RED","YELLOW","GREEN","BLUE","WHITE","BLACK") ;
document.write("<h2>Element of Array Before reverse()</h2><br/>");
for(i=0;i<Arr1.length;i++)
{
document.write(Arr1[i]+"<br/>");
}
Arr1.reverse();
document.write("<h2>Element of Array After reverse()</h2><br/>");
for(i=0;i<Arr1.length;i++)
{
document.write(Arr1[i]+"<br/>");
}

</script>

How to Make Array Java Script:


In many language array is collection of similar type of element but in java script it is different the value of element could be different type in Java script.

<script language="javascript">

Arr=new Array(2,3,4,5,"buy","sell") ;
for(i=0;i<=Arr.length-1;i++)
{
document.write(Arr[i]+"<br/>");
}
</script>

How to use continue statement in Java Script:


<script language="javascript">

i = 0;
n = 0;
while (i < 5)
{
   i++;
   if (i == 3)//3 will not be in output
      continue;
   n += i;
   document.writeln(i+"<br/>");
}

</script>

How to use break statement in Java Script:

 
<script language="javascript">
//run Script and watch what happened
var j=2;
i=1;
do
{
if(i>15)/*compiler come out from the loop if the value of i is greater than 15 */
break;
tab=i*j;
document.write(i+"*"+j+"="+tab+"<br/>");
i++;

}while(true);
</script>

How to use do while Loop in Java Script: Display 2’s Table:


Do while loop check the conditoin at the end of the loop so even the condition is true of false it must complile the loop code at least once. 

<script language="javascript">
var j=2;
i=1;
do
{
tab=i*j;
document.write(i+"*"+j+"="+tab+"<br/>");
i++;
}while(i<=10);
</script>