22 November, 2010

Basic javascript programs


 1. A javascript to implement variable usage
<html>
<title>Variable Using Script</title>
<body>
<script>
      var greetings;
      greetings='Hi, How are you?';
      alert(greetings);
</script>
</body>
</html>

2. A javascript to implement the pop-up boxes 
<html>
<title>Script for prompt</title>
<script>
var username;
var greeting;
username = prompt("What is your name?");
username=username.toUpperCase();
greeting="Hi, "+username;
alert(greeting);
</script>
</body>
</html>
</script>
</html>

3.Implementing the eval() function in javascript

          In javascript every input is read as string. We need to convert it to numbers for processing if the input is a number 
<html>
<title>Sum of Two Numbers</title>
<script>
var num1,num2;
var sum;
var msg;
num1=prompt("Enter the first number: ");
num1=eval(num1);
num2=prompt("Enter the second number: ");
num2=eval(num2);
sum=num1+num2;
msg="Sum= "+sum;
alert(msg);
</script></html>

4.  Using switch case in javascript.
<html>
<title>Switch Case</title>
<body>
<script>
       var num,day;
       num=prompt("Enter an integer: ");
       switch(num%7)
       {
              case 1: day="Sunday";break;
              case 2: day="Monday";break;
              case 3: day="Tuesday";break;
              case 4: day="Wednessday";break;
              case 5: day="Thursday";break;
              case 6: day="Friday";break;
              case 0: day="Saturday";break;
              default: day="Invalid Day Number!!\n";
       }
       alert("Day: "+day);
</script>
</body>
</html>

5. Implementing for loop in javascript
<html>
<title>For Loop</title>
<body>
<script>
       var i;
       for(i=0;i<10;i++)
       {
              alert("This is the message no: "+i);
       }
</script>
</body>
</html>

6. Generating local information from the browser using javascript
<html>
<title>Information</title>
<body>
<script>
       document.bgColor="blue";
       alert("Background: "+document.bgColor);
       alert("Domain: "+document.domain);
       alert("URL: "+document.location);
       alert("Last Modified: "+document.lastModified);
       alert("Last Page: "+document.referrer);
       alert("Title: "+document.title);
</script>
</body>
</html>

7.Writing dynamic javascript contents on to html page to display in browser 
<html>
<title>Dynamic Write</title>
<body>
<script>
       var name;
       name=prompt("Enter your name: ");
</script>
        Hi how are you?
<script>
       document.write(name);
</script>
</body>
</html>

8. Implementing onclick events and functions within a form using javascript
<html>
<title>Script for Form Variable Changing</title>
<body>
<script>
       function changebuttonValue()
       {
               document.myForm.button.value="changed";
       }
       function changeText()
       {
               document.myForm.text1.value="Enter you name";
       }
</script>
<form name=myForm>
<input type=text name=text1 onMouseOver='changeText();'>
<br>
<input type=button name='button' value=Enter onClick='changebuttonValue();'>
</form>
</body>
</html>

9. opening a new browser window using javascript
<html>
<title>Open a New Window</title>
<body>
<input type=button value="Show New Window" onClick="newWindow();">
<input type=button value="Close Window" onClick="closeWindow();">
<script>
       var new1;
       function newWindow()
       {
              //document.write("Hi");
              new1=window.open("frame1.html","Name","height=450,width=450");
              new1.focus();
       }
       function closeWindow()
       {
              new1.close();
       }
</script>
</body>
</html><hr align="left" size="1" width="100%">


0 comments:

Post a Comment

speak out... itz your time !!!

 

Terminal Diary | techblog Copyright © 2009 Gadget Blog is Designed by jintu jacob Powered by Blogger