Saturday, 31 March 2012

JAVASCRIPT


Ways to implement JAVAScript
1 ) <script type="text/javascript" src="xxx.js"></script>
2 ) <script type ="text/javascript">
    function display() {
        document.write("<p>" + Date() + "</p>");
    }

</script>
    <title></title>
</head>

<body>
<button id  ="b1" runat ="server" onclick ="display()" value ="click">Click</button>

3) Semicolon is optional

JavaScript Statements

A JavaScript statement is a command to a browser. The purpose of the command is to tell the browser what to do.
This JavaScript statement tells the browser to write "Hello Dolly" to the web page:
document.write("Hello Dolly");
It is normal to add a semicolon at the end of each executable statement. Most people think this is a good programming practice, and most often you will see this in JavaScript examples on the web.
The semicolon is optional (according to the JavaScript standard), and the browser is supposed to interpret the end of the line as the end of the statement. Because of this you will often see examples without the semicolon at the end.

4)  Conditional Operator
<script type="text/javascript">

var visitor="PRES";
var greeting=(visitor=="PRES")?"Dear President ":"Dear ";
document.write(greeting);

</script>

5) Conditional
 <script type="text/javascript">
//Write a "Good morning" greeting if
//the time is less than 10

var d=new Date();
var time=d.getHours();

if (time<10)
  {
  document.write("<b>Good morning</b>");
  }
</script>
6)  Switch case

<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>
7) Confirm functionality
function show_confirm() {
        var r = confirm("Press a button");
        if (r == true) {
            alert("You pressed OK!");
        }
        else {
            alert("You pressed Cancel!");
        }
    }
8)  For….In
<html>
<body>
<script type="text/javascript">

var person={fname:"John",lname:"Doe",age:25};
var x;
for (x in person)
{
document.write(person[x] + " ");
}
</script>
</body>
</html>
Output : John Doe 25

No comments:

Post a Comment