Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
COMPILES/EXECUTES IN
BROWSER
About JavaScript
<html>
<body>
<script type="text/javascript">
<document.write("Hello World!");
</script>
</body>
</html>
Hello World!
JavaScript Code
<script type="text/javascript">
document.write("<h1>This is a
header</h1>");
document.write("<p>This is a
paragraph</p>");
document.write("<p>This is another
paragraph</p>");
</script>
Output of the above written code:-
This is a header
This is a paragraph
This is another paragraph
JavaScript Blocks
Blocks start with a left curly bracket {, and ends with a right curly bracket }.
This example will write a header and two paragraphs to a web page:
Example:-
<script type="text/javascript">
{
document.write("<h1>This is a header</h1>");
document.write("<p>This is a paragraph</p>");
document.write("<p>This is another paragraph</p>");
}
</script>
Output of the above written code:-
This is a header
This is a paragraph
This is another paragraph
JavaScript Comments
A variable can have a short name, like x, or a more describing name like length.
Variable names are case sensitive (y and Y are two different variables)
Variable names must begin with a letter or the underscore character
NOTE: Because JavaScript is case-sensitive, variable names are case-sensitive.
Example of Javascript Variables
<html>
<body>
<script type="text/javascript">
var firstname;
firstname="Hege";
document.write(firstname);
document.write("<br />");
firstname="Tove";
document.write(firstname);
</script>
<p>The script above declares a variable,
assigns a value to it, displays the value, change the value,
and displays the value again.</p>
</body>
</html>
Output of the above written code:-
Hege
Tove
The script above declares a variable, assigns a value to it,
displays the value, change the value, and displays the value
again
Variables
var x;
var carname;
After the declaration shown above, the variables has no values, but you can
assign values to the variables while you declare them:
var x=5;
var carname="Volvo";
Assigning Values to Undeclared
JavaScript Variables
= x=y x=5
+= x+=y x=x+y x=15
-= x-=y x=x-y x=5
*= x*=y x=x*y x=50
/= x/=y x=x/y x=2
%= x%=y x=x%y x=0
The + Operator Used on Strings
The + operator can also be used to add string variables or text values together.
To add two or more string variables together, use the + operator.
<script type="text/javascript">
x=5+5;
document.write(x);
document.write("<br />");
x="5"+"5";
document.write(x);
document.write("<br />");
x=5+"5";
document.write(x);
document.write("<br />");
x="5"+5;
document.write(x);
document.write("<br />");
</script>
</body>
</html>
Output of the above written code:-
10
55
55
55
The common rule is: If you add a number and a
string, the result will be a string.
Comparison Operators
if (age<18) document.write("Tooyoung");
Logical Operators
Logical operators are used in determine the logic between variables or values.
Given that x=6 and y=3, the table below explains the logical operators:
Example
Syntax
variablename=(condition)?value1:value2
If the variable visitor has the value of "PRES", then the variable greeting
will be assigned the value "Dear President " else it will be assigned "Dear".
Example of If statements
<html>
<body>
<script type="text/javascript">
var d = new Date();
var time = d.getHours();
<p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
</p>
</body>
</html>
Output of above code
<p>
This example demonstrates the If...Else statement.
</p>
<p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
Otherwise you will get a "Good day" greeting.
</p>
</body>
</html>
Output of above code
Good day
This example demonstrates the If...Else statement.
If the time on your browser is less than 10, you will
get a "Good morning" greeting. Otherwise you will
get a "Good day" greeting.
Example of If..else if...else statement
<html>
<body>
<script type="text/javascript">
var d = new Date();
var time = d.getHours();
if (time<10)
{
document.write("<b>Good morning</b>");
}
else if (time>=10 && time<16)
{
document.write("<b>Good day</b>");
}
else
{
document.write("<b>Hello World!</b>");
}
</script>
<p>
This example demonstrates the if..else if...else statement.
</p>
</body>
</html>
Output of above code
Hello World!
This example demonstrates the if..else if...else
statement.
Example of Random Link
<html>
<body>
<script type="text/javascript">
var r=Math.random();
if (r>0.5)
{
document.write("<a href='http://www.w3schools.com'>Learn Web Development!</a>");
}
else
{
document.write("<a href='http://www.refsnesdata.no'>Visit HPES Data!</a>");
}
</script>
</body>
</html>
<p>This JavaScript will generate a different greeting based on what day it is. Note that Sunday=0,
Monday=1, Tuesday=2, etc.</p>
</body>
</html>
Output of above code
An alert box is often used if you want to make sure information comes through to the
user.
When an alert box pops up, the user will have to click "OK" to proceed.
</body>
</html>
Example of Confirm Box
Confirm Box
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.
<html>
<head>
<script type="text/javascript">
function disp_confirm()
{
var r=confirm("Press a button");
if (r==true)
{
document.write("You pressed OK!");
}
else
{
document.write("You pressed Cancel!");
}
}
</script>
</head>
<body>
</body>
</html>
Example of Prompt Box
Prompt Box
A prompt box is often used if you want the user to input a value before entering a page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to
proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the
box returns null.
To Input the value in run time
<html>
<head>
<script type="text/javascript">
function disp_prompt()
{
var name=prompt("Please enter your name","Harry Potter");
if (name!=null && name!="")
{
document.write("Hello " + name + "! How are you today?");
}
}
</script>
</head>
<body>
<input type="button" onclick="disp_prompt()" value="Display a prompt box" />
</body>
</html>
Example of new window open
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function show()s
{
window.open("Default2.aspx","chandreshsingh","top=40,left=50,width=50,height=50,resizable=1,to
olbar=1");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="btn" onclick="show()" value="Example of open new window"
style="left: 354px; position: relative; top: 58px; color: #ff0066; height: 77px;" size="40"/></div>
</form>
</body>
</html>
Example to print the
keycode/keyvalue of each key
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function WhichKeyPress(event)
{
alert(event.keyCode);
}
</script>
</head>
<body onkeydown="WhichKeyPress(event)">
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
AJAX
ScriptManager:-
Manages script resources for client components, partial-page
rendering, localization, globalization, and custom user
scripts. The ScriptManager control is required in order to
use the UpdatePanel, UpdateProgress, and Timer controls.
AJAX
UpdatePanel:-
Enables you to refresh selected parts of the page,
instead of refreshing the whole page by using a
synchronous postback.
UpdateProgress :-
Provides status information about partial-page
updates in UpdatePanel controls.
AJAX
Timer:-
Performs postbacks at defined intervals. You can use
the Timer control to post the whole page, or use it
together with the UpdatePanel control to perform
partial-page updates at a defined interval.