Javascript: Web Technologies - by G.Sreenivasulu
Javascript: Web Technologies - by G.Sreenivasulu
Javascript: Web Technologies - by G.Sreenivasulu
Sreenivasulu Handout - 2
JAVASCRIPT
JavaScript is a new scripting language which is being developed by Netscape.JavaScript
is a compact, object-based scripting language for Web pages
JavaScript and Java
Although the names are almost the same, JavaScript isn't the same as Java. These are
two different techniques for Internet programming. Java is a real programming language,
and you can create real programs with it.JavaScript is a scripting language. You could
even say that JavaScript is rather an extension to HTML than a separate computer
language. It's so tightly integrated with HTML that you could call it "JavaScript markup
language." JavaScript coders don't care too much about real programming, they just make
different nice effects by inserting small JavaScript code fragments into their Web pages.
JavaScript can be inserted into a HTML file by using <SCRIPT> tag or can be created as a sepetate
.js file and can be inserted into the HTML
myscript.js
function popup() {
alert("Hello World")
}
Example :
<html>
<body>
<script
type="text/JavaScript"> <!--
document.write("Hello
World!") //-->
</script>
</body>
</html>
Note: The <!-- and --> tag as Old browsers will not understand the <SCRIPT> tags.
Department of CSE , JBIET
Web Technologies - by G.Sreenivasulu Handout - 2
Dialog boxes
JavaScript has three kind of popup (dialog) boxes Alert box, Confirm box, and Prompt box.
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>
<body>
<script type="text/javascript">
alert("Welcome to my
world!!!"); </script>
</body>
</html>
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.
Example
<html>
<head>
<script type="text/javascript">
function show_confirm() {
var r=confirm("Press
a button"); if (r==true)
{ document.write("You pressed OK!");
}
else
{ document.write("You pressed Cancel!");
}}
</script>
</head>
<body>
<input type="button" onclick="show_confirm()" value="Show
confirm box" /> </body>
</html>
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.
Example
<html>
function disp()
{
var name=prompt("Please enter your
name","Sreenivas"); if (name!=null && name!="")
{
document.write("Hello " + name + "! How are you today?");
}
}
</script>
</head>
<body>
<input type="button" onclick="disp()" value="Show
prompt box" /> </body>
</html>
while loop
example: i = 1;
while (i<=5) {
document.write("\t"+i);
i = i + 1;
}
switch statement
Switch statements are used to select which statements are to be executed depending
on a variable's value matching a label.
Syntax
function functionname(var1,var2,...,varX)
{
some code
}
Example
<html>
<head>
<script type="text/javascript">
function product(a,b)
{
return a*b;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product(4,3));
</script>
</body>
</html>
Example
This is a very simple script. It opens up an alert message box which displays whatever is
typed in the text box.
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function MsgBox (textstring) {
alert (textstring) }
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<INPUT NAME="text1" TYPE=Text>
<INPUT NAME="submit" TYPE=Button VALUE="Show
Me" onClick="MsgBox(form.text1.value)">
</FORM>
</BODY>
</HTML>
Department of CSE , JBIET
Web Technologies - by G.Sreenivasulu Handout - 2
<HTML>
<HEAD>
<SCRIPT>
function getSelect(s) {
return s.options[s.selectedIndex].value
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<SELECT NAME="list" SIZE=1 OnChange="location=getSelect(this)">
<OPTION value="#"> Choose a search engine
<OPTION value="http://www.jbiet.edu.in"> JBIET
<OPTION value="http://www.jbrec.edu.in"> JBREC
<OPTION value="http://www.bhaskarmedicalcollege.edu.in/">
BMC </SELECT>
<FORM>
</BODY>
</HTML>
<HTML>
<HEAD>
<script language="javascript">
function validate(value) {
if (value<0)
alert("Please input a value that
\n"+ " is greater or equal to 0");
}
</script>
</HEAD>
<BODY>
Try inputting a value less
than zero <FORM>
<INPUT TYPE="text"
onBlur="validate(this.value)">
</FORM> </BODY>
</HTML>
<html>
<head>
<script
language="javascript">
function showElement()
{
var txt=document.getElementById("txt");
alert(txt.value);
var
t=document.getElementsByName("txt");
alert(t.value);
}
</script>
</head>
<body>
<input type="text" name="txt" id="txt"><br><br>
<input type="button" value="Enter Text and Click
Me" onclick="showElement()"> </body>
</html>
JAVASCRIPT OBJECTS
Genwral Ojects of JavaScript are Array, Date, Function,Math, String etc. Also we
have Window ,History, Navigator , Document , Form Objects.
document.write(str.toLowerCase());
document.write(str.substr(2,3));
document.write(str.charAt(1));
document.write(str.length);
document.write(str.replace("world", "hyderabad"));
Math Object
document.writeln(Math.abs(7.25));
document.writeln(Math.ceil(0.60));
document.writeln(Math.floor(0.60));
document.writeln(Math.round(0.49);
document.writeln(Math.cos(0));
document.writeln(Math.max(5,7));
document.writeln(Math.min(5,7));
document.writeln(Math.pow(2,3));
document.writeln(Math.random())); //returns a random number between
0 and 1. document.writeln(Math.sin(Math.PI/2));
document.writeln(Math.sqrt(9));
Document Object
document.writeln(document.title);
document.writeln(document.URL);
document.writeln(document.domain);
document.writeln(document.anchors.length); // will give no of
anchor elemets document.bgColor = "#FF00FF';
document.writeln(document.forms.length);
document.writeln(document.images.length);
Department of CSE , JBIET
Web Technologies - by G.Sreenivasulu Ha
Department of CSE , JBIET