Javascript : Compiles/Executes in Browser
Javascript : Compiles/Executes in Browser
Javascript : Compiles/Executes in Browser
COMPILES/EXECUTES IN
BROWSER
About JavaScript
JavaScript is a Scripting Language.
JavaScript is the Open Source Element.
JavaScript is Platform Independent Language.
JavaScript is the Case Sensitive Language.
JavaScript is developed by the NetScape Corporation.
JavaScript is the Object Base Language.
JavaScript is the Event-Driven Programming Language.
JavaScript is mainly used for client site validations.
JavaScript is also known as LiveScript.
If JavaScript support to the server site in this situation also called
LiveWire.
Every Person can be used without purchasing the License.
<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
JavaScript Code
JavaScript code (or just JavaScript) is a
sequence of JavaScript statements.
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>
This is a paragraph
This is another paragraph
JavaScript Comments
Comments can be added to explain the JavaScript, or to make it
more readable.
Single line comments start with //.
This example uses single line comments to explain the code:
<script type="text/javascript">
// This will write a header:
document.write("<h1>This is a header</h1>");
// This will write two paragraphs: document.write("<p>This is a
paragraph</p>"); document.write("<p>This is another paragraph</p>");
</script>
JavaScript Multi-Line
Comments
Multi line comments start with /* and end with */.
This example uses a multi line comment to explain the
code:
<script type="text/javascript">
/*
The code below will write one header and two paragraphs
*/
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>
JavaScript Variables
expressions.
A variable can have a short name, like x, or a more describing name like
length.
carname="Volvo".
sensitive.
<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>
Tove
The script above declares a variable, assigns a value to it,
displays the value, change the value, and displays the value
again
Variables
variables.
var x=5;
var carname="Volvo";
Example
Result
Addition
x=y+2
x=7
Subtraction
x=y-2
x=3
Multiplication
x=y*2
x=10
Division
x=y/2
x=2.5
Modulus (division
remainder)
x=y%2
x=1
++
Increment
x=++y
x=6
--
Decrement
x=--y
x=4
Operator
Continue..
Assignment operators are used to assign values to JavaScript variables.
Given that x=10 and y=5, the table below explains the assignment operators:
Operator
Example
Same As
Result
x=y
+=
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
x=5
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.
txt1="What a very"; txt2="nice day"; txt3=txt1+txt2;
Continue
or insert a space into the expression:
<html>
<body>
<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>
55
55
55
The common rule is: If you add a number and a
string, the result will be a string.
Comparison Operators
Description
Example
==
is equal to
x==8 is false
===
x===5 is true
x==="5" is false
!=
is not equal
x!=8 is true
>
is greater than
x>8 is false
<
is less than
x<8 is true
>=
x>=8 is false
<=
x<=8 is true
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:
Operator
Description
Example
&&
and
||
or
not
!(x==y) is true
Example
greeting=(visitor=="PRES")?"Dear President ":"Dear ";
Conditional Operator
JavaScript also contains a conditional operator that assigns a value to a
variable based on some condition.
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>
html>
<body>
<script type="text/javascript">
var d = new Date();
var time = d.getHours();
<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>
<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>
<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>
<html>
<body>
<script type="text/javascript">
var d = new Date();
theDay=d.getDay();
switch (theDay)
{
case 5:
document.write("<b>Finally Friday</b>");
break;
case 6:
document.write("<b>Super Saturday</b>");
break;
case 0:
document.write("<b>Sleepy Sunday</b>");
break;
default:
document.write("<b>I'm really looking forward to this weekend!</b>");
}
</script>
<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>
Alert Box
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.
<html>
<head>
<script type="text/javascript">
function disp_alert()
{
alert("Hello again! This is how we" + '\n' + "add line breaks to an alert
box!");
}
</script>
</head>
<body>
</body>
</html>
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!");
}
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.
<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>
</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>
AJAX
AJAX
AJAX
AJAX uses asynchronous data transfer (HTTP requests)
software.
AJAX
AJAX is Based on Web Standards
AJAX is based on the following web standards:
JavaScript
XML
HTML
CSS
AJAX
Ajax Server Controls:ASP.NET AJAX server controls consist of server and client
code that integrate to produce rich client behavior. When
you add an AJAX control to an ASP.NET Web page, the
page automatically sends supporting client script to the
browser for AJAX functionality. You can provide additional
client code to customize the functionality of a control, but
this is not required.
AJAX
The following list describes the most frequently used
ASP.NET AJAX server controls.
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.