You are on page 1of 5

DYNAMIC PROGRAMING IN HTML USING JAVASCRIPT

SCRIPTING LANGUAGE

1. light weight programming languagewith less complexity.


2. Other examples are ASP, php.
3. Used for making website interactive and add dynamic nature in the website.

JAVA SCRIPT

1. both client and server-side based scripting language.


2. Interpreted language, interpreted by the javascript interpreter which is inbuilt in browser.
3. Client-side JS allow you to validate only thoes program that executes and produce result on
client machine.
4. Server-side JS validates only thoes program that execute on the server.
5. Includes various built-in objects.
6. Platform independent.

FEATURES OF JAVASCRIPT

1. imperitive and structured: supports all syntax of structured programming language


2. functional: does not support classes. Objectes created from constructor functions.
3. prototype based: uses prototypes instead of classes for inhertiance
4. platform independent: can run on any system.

Using JavaScript in an HTML Document

1. add javascript using <SCRIPT>


2. <SCRIPT> can contain javascript code or can connect to an external java script file.
3. Contains five attributes:
a. async: true/false
b. type: specify MIMEtype of script(text/javascript,text/vbscript)
c. charset
d. src
e. defer
4. three ways of adding script:
a. in head
b. in body
c. as an external script.

JS in head : placed in head when you performsome action such as click of submit button or link.
JS in body : this will execute when a web page start loading in a web browser.
JS in external file : when code is lengthy and for code reusability. Added using src attribute.

Programing fundamentals of JavaScript

1. Lexical structure: provide set of rules to write program, defines rules for variable name,
writing comments, procedure to seprate one statement with other. Define rule for following
character set: total 128, 96 characters availabe, 32 reserved control charcters. \t,\f,\b,\n,\,\\
case senstivity: marks, Marks, MARKS all are different words.
white space and line break: matters in case ofstrings and regular expressions.
optional semicolon
comments: // single line comments /*multiple line comment*/
literal :
1. numerical decimal(89,569), octal(021,065), hexadecimal(0x5673)
2. floating point - 4.987, -123.5679, 0.98
3. boolean true or false
4. string - akhil or 'akhil'
5. array declared in [ ] brackets. var emp=['a','b','c']
6. regular expression RegExp used to match a character or string.
7. object literal collection of name-value pair enclosed in { } separated by , .

identifier
1. must start with letter, $ , _ .
2. cannot start with number.
3. any length.
4. case sensitive
5. cannot contain reserved words.

reserved words
1. already defined in javascript.

2. Exploring variables
data stored in variables.
Variable has name, value and address
name uniquely identify variable, value refers to data,memory refers to memory location of
variable.
Declaring a variable using var keyword: var abc

3. Exploring operators
+ , - , * , % ,++ , --, += , -= , == , && , !! and many more

4. Exploring control flow statements


selection statement if , else, switch.
Loops while , do while, for loop
Jump break, continue.

5. Exploring popup boxes


alert OK button.
confirm OK and CANCEL button. If ok pressed value passed is true and if cancel is
pressed value send is false
prompt OK and CANCEL button. If any text is entered then that valu will be passed else
an empty string is passed.
Examples

1. adding script in head, body and using an external java script file

<html>
<head>
<script type="text/javascript" >
document.write("written in head of the page");
</script>
</head>

<body>
<script type="text/javascript" >
document.write("<br>written in body of the page<br>");
</script>

<script src="firstjs.js" >


</script>
</body>
</html>

firstjs.js (external javscript file)

var a=10;
for(i=0;i<5;i++)
{
a=a+1;
}
document.write(a);

2. Basic operations and operators in java script

<html>
<body>
<hr>
<script type="text/javascript" >
document.write("Simple value printing <br><br>");
var a=10;
document.write("Value of a = "+a);
</script>
<hr>

<script type="text/javascript" >


document.write("<br>assigning value of one variable to other<br><br> ");
var b=10;
var c=b;
document.write("Value of b= "+b+ " value of c= "+c)
</script>
<hr>

<script type="text/javascript" >


document.write("operators<br><br>");
var a=10,b=20,c=30;
document.write("average of "+a+","+b+","+c+" is = ");
avg=(a+b+c)/3;
document.write(avg);
</script>
<hr>

<script type="text/javascript" >


var a=[1,2,3,4,5,6,7,8,9,10];
var i=0;
for(i=0;i<10;i++)
{
if(a[i]%2==0)
{
document.write("<br><br>"+a[i]+" is even");
}
else
{
document.write("<br><br>"+a[i]+ " is odd");
}
}
</script>

<hr>

</body>
</html>

3. Alert box

<html>
<body>
<script type="text/javascript" >
alert("hello i am in alert");
</script>
</body>
</html>

4. Confirm box

<html>
<body>
<script type="text/javascript" >
var q=confirm("Press any one button ")
if(q)
{
document.write("you pressed ok");
}
else
{
document.write("you pressed cancel ");
}
</script>
</body>
</html>

5. Prompt box

<html>
<body>
<script type="text/javascript" >
var n= prompt("enter name");
document.write(n);
</script>
</body>
</html>

You might also like