Find us on Google+ Kill the code: Javascript
Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Thursday, 24 May 2012

Surfing on a html page using iframe

Preview :








Code:


<html>
   <head>
      <script type="text/javascript">
         function surf() {
            document.getElementById('web').src=document.getElementById('url').value;
         }
      </script>


   </head>
   <table>
      <tr>
         <td>
            <input type="text" id="url" /><input type="button" id="sruf" value="surf" onclick="return surf()"/>
         </td> 
      </tr>
   </table>

   <iframe height="400px" width="400px" src="http://www.killthecode.blogspot.com" id="web"> </iframe>    



</html>

Saturday, 17 March 2012

Calculator using Buttons in JavaScript

Untitled Document


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
.box {
 width:15px;
 height:15px;
}
</style>
</head>

<body>
<script type="text/javascript">
 var no1="",no2="";
 var flag=0;
 var flag2=0;
 var already=0;
 function calc(id,txt)
 {
  
  if(flag==0)
  {
   no1=no1+document.getElementById(id).value;
   document.getElementById(txt).value=no1;
  }
  else if(flag==1)
  {
   no2=no2+document.getElementById(id).value;
   document.getElementById(txt).value=no2;
  }
 }
 function change(id)
 {
  if(already==0)
  {
  flag=1;
  already=1;
  var sign=document.getElementById(id).value;
  
  if(sign=='+')
  {
   if(flag2==0) document.getElementById('one').value=document.getElementById('two').value;
   document.getElementById('one').value=document.getElementById('one').value+"+";
   flag2=1;
  }
  if(sign=='-')
  {
   if(flag2==0) document.getElementById('one').value=document.getElementById('two').value;
   document.getElementById('one').value=document.getElementById('one').value+"-";
   flag2=1;
  }
  if(sign=='*')
  {
   if(flag2==0) document.getElementById('one').value=document.getElementById('two').value;
   document.getElementById('one').value=document.getElementById('one').value+"*";
   flag2=1;
  }
  if(sign=='/')
  {
   if(flag2==0) document.getElementById('one').value=document.getElementById('two').value;
   document.getElementById('one').value=document.getElementById('one').value+"/";
   flag2=1;
  }
  document.getElementById('two').value="";
  }
  
  
 }
 function ans(id)
 {
  var txt1=document.getElementById('one').value;
  var no1=txt1.substr(0,txt1.length-1);
  var no2= document.getElementById('two').value;
  flag=3;
  
  <!--var no2=document.getElementById('two').value;-->
  
  <!--var txt1=document.getElementById('one').value;-->
  <!--var no2=no1.substr(0,txt1.length-2);-->
  no1=parseInt(no1);
  no2=parseInt(no2);
  
  if(txt1.charAt(txt1.length-1)=='+') document.getElementById('ans').value=(no1+no2);
  if(txt1.charAt(txt1.length-1)=="-") document.getElementById('ans').value=(no1-no2);
  if(txt1.charAt(txt1.length-1)=="*") document.getElementById('ans').value=(no1*no2);
  if(txt1.charAt(txt1.length-1)=="/") 
  {
   try 
   {
    if(no2!=0) document.getElementById('ans').value=(no1/no2);
    else  document.getElementById('ans').value="Error while dividing";
   }
   catch(err)
   {
    document.getElementById('ans').value="by Zero is not possible";
   }
  }
  
 }
 function clear1()
 {
  document.getElementById('one').value="no1";
  document.getElementById('two').value="no2"
  document.getElementById('ans').value="answer";
  no1="";
  no2="";
  flag=0;
  flag2=0;
  already=0;
 }
 
</script>

<input type="text" id="one" readonly="readonly" value="no1"/><br />
<input type="text" id="two" readonly="readonly" value="no2"/><br />
<input type="text" id="ans" value="answer" readonly="readonly" />

<table width="30%" cellpadding="0" cellpadding="0">
<tr>
  <td><input type="button" id="01" value="1" onclick="return calc(this.id,'two')"/>
  <input type="button" id="2" value="2" onclick="return calc(this.id,'two')"/>
  <input type="button" id="3" value="3" onclick="return calc(this.id,'two')"/>
  <input type="button" id="add" value="+" onclick="return change(this.id)" />
 </td>
</tr>
<tr>
<td>
  <input type="button" id="4" value="4" onclick="return calc(this.id,'two')"/>
  <input type="button" id="5" value="5" onclick="return calc(this.id,'two')"/>
  <input type="button" id="6" value="6" onclick="return calc(this.id,'two')"/>
  <input type="button" id="sub" value="-" onclick="return change(this.id)" />
</td>
</tr>
<tr>
<td>
  <input type="button" id="7" value="7" onclick="return calc(this.id,'two')"/>
  <input type="button" id="8" value="8" onclick="return calc(this.id,'two')"/>
  <input type="button" id="9" value="9" onclick="return calc(this.id,'two')"/>
  <input type="button" id="mul" value="*" onclick="return change(this.id)" />
</td>
</tr>
<tr>
<td>
 <input type="button" id="c" value="C" onclick="return clear1()"/>
 <input type="button" id="0" value="0" onclick="return calc(this.id,'two')"/>
 <input type="button" id="equal" value="=" onclick="return ans(this.id)"/>
 <input type="button" id="div" value="/" onclick="return change(this.id)" />
</td>
</tr>
</table>


</body>
</html>


Monday, 12 March 2012

Count the number of Checkboxes selected

Untitled Document
Subject one two three four five

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<script type="text/javascript">
var i=0;
function isset(id) {
var n=document.getElementById(id);
if(n.checked==true) {
i++;
}
else {
i--;
}
}
function validate() {
document.getElementById('lbl').innerHTML=i+" Checkbox Selected";
}
</script>


<table width="100%"><br />
<tr>
<td width="20%">Subject </td>
<td> <input type="checkbox" id="1" onclick="return isset(this.id)" />one
<input type="checkbox" id="2" onclick="return isset(this.id)" />two
<input type="checkbox" id="3" onclick="return isset(this.id)" />three
<input type="checkbox" id="4" onclick="return isset(this.id)" />four
<input type="checkbox" id="5" onclick="return isset(this.id)" />five
</td>
<td> <input type="button" onclick="return validate()" value="Check"/> </td>
<td> <label id="lbl"> </label> </td>
</tr>
</table>

</body>
</html>


Sunday, 11 March 2012

Palindrome validation in Javascript

Preview :


New Web Project Enter a string :


Code :

<html>

    <head>

        <title>New Web Project</title>

    </head>

    <body>

     

     <script type="text/javascript">

      function pal() {

       var b;

       var a = document.getElementById('str');

       var c1 = a.value.length;

       var flag=0;

       for(b=0; b<c1/2;b++) {

        if(a.value.charAt(b)==a.value.charAt(--c1)) {

         flag=0;

        }

        else {

          flag=1;
          break;

        }

       }

       if(flag!=1) {

        document.getElementById("lbl").innerHTML = "The string is Palindrome";

        document.getElementById("fnt").style.color = "green";

       }

       else {

        document.getElementById("lbl").innerHTML = "The string is not Palindrome";

        document.getElementById("fnt").style.color = "red";

       }

       return;

      }

      

      function foc() {

       var a = document.getElementById("str");

       if(a.value == "enter string...") a.value = "";

       return;

      }

      function foc_lost() {

       var b = document.getElementById("str");

       if(b.value == "") b.value = "enter string...";

       return;

      }

     </script>

     

     

     

     

        Enter a string : <input type="text" name="string" id="str" value="enter string..." onfocus="return foc()" onblur="return foc_lost()"/>

        <input type="button" onclick="return pal()" value="Check"/><br />

        <font id="fnt"><label id="lbl"></label></font>

    </body>

</html>

Sunday, 4 March 2012

Image changer in Javascript using Z-Index

Preview :
New Web Project







Code :







<html>
    <head>         <title>New Web Project</title>         <style type="text/css">         #block img {         position:absolute;         left:10px;         top:10px;         z-index:0;         }         #but {         margin-left: 300px;         margin-top: 300px;         }         </style>     </head>     <body>     <script type="text/javascript">     var b = 0;     function change() {     b++;     var a = document.getElementById(b%5);     a.style.zIndex = b;     return;     }     function call() {     var c = setInterval("change()",1000);     return;     }     </script>         <div id="block">         <img src="4.png" id="4"/>         <img src="3.png" id="3"/>         <img src="2.png" id="2"/>         <img src="1.png" id="1"/>         </div>         <input type="button" id="but" onclick="return call()" value="change">     </body> </html>

Thursday, 1 March 2012

Image Swaping in Javascript

Preview : 


New Web Project




Code :




<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>New Web Project</title>
    </head>
    <body>
    <script type="text/javascript">
    function swap() {
    var t = document.getElementById("i1").src;
    document.getElementById("i1").src = document.getElementById("i2").src;
    document.getElementById("i2").src = t;
    return;
    }
    </script>
        <img src="1.png" id="i1"/>
        <img src="2.png" id="i2"/>
        <input type="button" id="but" value="swap" onclick="return swap()"/>
        <a href="http://killthecode.blogspot.in">For more programs click here</a>
    </body>
</html>

Thursday, 23 February 2012

Simple Calculator in Javascript using Switch Case


<html>
<head>
</head>
<body>
<script type="text/javascript">
function all(value1) {

var a1 = parseInt(document.getElementById("n1").value);
var a2 = parseInt(document.getElementById("n2").value);
var a3;
switch(value1)
{
case '+' : a3=a1+a2; break;
case '-' : a3=a1-a2; break;
case '*' : a3=a1*a2; break;
case '/' : a3=a1/a2; break;
}
document.getElementById("ans").value=a3;
return;
}
</script>
<table align="center">
<tr>
<td colspan="2">
<input type="text" id="n1"/>
</td>
<td colspan="2">
<input type="text" id="n2"/>
</td>
</tr>
<tr>
<td>
<input type="button" value="+" id="+but" onclick=" return all(this.value)" value="+"/>
</td>
<td>
<input type="button" value="-" id="-but" onclick=" return all(this.value)" value="-"/>
</td>
<td>
<input type="button" value="*" id="*but" onclick=" return all(this.value)" value="*"/>
</td>
<td>
<input type="button" value="/" id="/but" onclick=" return all(this.value)" value="/"/>
</td>
</tr>
<tr>
<td colspan="4">
<input type="text" id="ans" readonly="readonly"/>
</td>
</tr>
</table>
</body>
</html>

Sunday, 19 February 2012

Simple Calculator in JavaScript

calc
Preview :
Code :


<html>
<head>
<title>calc</title>
</head>
<body>
<script type="text/javascript">
function add() {
var a1 = document.getElementById('n1');
var a2 = document.getElementById('n2');
document.getElementById('ans').value = parseInt(a1.value)+parseInt(a2.value);
return;
}
function sub() {
var a1 = document.getElementById('n1');
var a2 = document.getElementById('n2');
document.getElementById('ans').value = parseInt(a1.value)-parseInt(a2.value);
return;
}
function mul() {
var a1 = document.getElementById('n1');
var a2 = document.getElementById('n2');
document.getElementById('ans').value = parseInt(a1.value)*parseInt(a2.value);
return;
}
function div() {
var a1 = document.getElementById('n1');
var a2 = document.getElementById('n2');
document.getElementById('ans').value = parseInt(a1.value)/parseInt(a2.value);
return;
}
function all(id) {
if(id=='+but') add();
if(id=='-but') sub();
if(id=='*but') mul();
if(id=='/but') div();
return;
}
</script>
<table align="center">
<tr>
<td colspan="2">
<input type="text" id="n1"/>
</td>
<td colspan="2">
<input type="text" id="n2"/>
</td>
</tr>
<tr>
<td>
<input type="button" value="+" id="+but" onclick=" return add('+but')"/>
</td>
<td>
<input type="button" value="-" id="-but" onclick=" return sub('-but')"/>
</td>
<td>
<input type="button" value="*" id="*but" onclick=" return mul('*but')"/>
</td>
<td>
<input type="button" value="/" id="/but" onclick=" return div('/but')"/>
</td>
</tr>
<tr>
<td colspan="4">
<input type="text" id="ans" readonly="readonly"/>
</td>
</tr>
</table>
</body>
</html>