Web Design Practical Theory
Web Design Practical Theory
You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that
wraps around JavaScript code.
The <script> tag can be placed in the <head> section of your HTML or in the <body> section,
depending on when you want the JavaScript to load.
Generally, JavaScript code can go inside of the document <head> section in order to keep them
contained and out of the main content of your HTML document.
However, if your script needs to run at a certain point within a page’s layout — like when
using document.write to generate content — you should put it at the point where it should be called,
usually within the <body> section.
Practical 3
Write a JavaScript code to demonstrate Looping Statements
Types of Loops
There are mainly four types of loops in JavaScript.
1. for loop
2. for/in a loop (explained later)
3. while loop
4. do…while loop
for loop
Syntax:
for(statement1; statement2; statment3)
{
lines of code to be executed
}
1. The statement1 is executed first even before executing the looping code. So, this statement is
normally used to assign values to variables that will be used inside the loop.
2. The statement2 is the condition to execute the loop.
3. The statement3 is executed every time after the looping code is executed.
while loop
Syntax:
while(condition)
{
lines of code to be executed
}
The “while loop” is executed as long as the specified condition is true. Inside the while loop, you should
include the statement that will end the loop at some point of time. Otherwise, your loop will never end
and your browser may crash.
do…while loop
Syntax:
do
{
block of code to be executed
} while (condition)
The do…while loop is very similar to while loop. The only difference is that in do…while loop, the block
of code gets executed once even before checking the condition.
Practical 4 : Write JavaScript code to demonstrate different string functionsString Methods
Methods Description
charCodeAt() It provides the Unicode value of a character present at the specified index.
lastIndexOf() It provides the position of a char value present in the given string by searching
a character from the last position.
search() It searches a specified regular expression in a given string and returns its
position if a match occurs.
match() It searches a specified regular expression in a given string and returns that
regular expression if a match occurs.
substr() It is used to fetch the part of the given string on the basis of the specified starting
position and length.
substring() It is used to fetch the part of the given string on the basis of the specified index.
slice() It is used to fetch the part of the given string. It allows us to assign positive as
well negative index.
toLocaleLowerCase() It converts the given string into lowercase letter on the basis of host?s current
locale.
toLocaleUpperCase() It converts the given string into uppercase letter on the basis of host?s current
locale.
split() It splits a string into substring array, then returns that newly created array.
trim() It trims the white space from the left and right side of the string.
Practical 5
Write JavaScript code to demonstrate onblur, onfocus, onload, onsubmit.
Form events:
Event Performed Event Handler Description
change onchange When the user modifies or changes the value of a form
element
mouseover onmouseover When the cursor of the mouse comes over the
element
mousedown onmousedown When the mouse button is pressed over the element
mouseup onmouseup When the mouse button is released over the element