Topic 5 CSS
Topic 5 CSS
Frames
</script>
</head>
<body>
<h2>Example of Regular Expression</h2>
<script type="text/javascript">
var input_str=prompt("Enter some string here","");
TestString(input_str);
</script>
</body>
</html>
TM
Technical Publications - An up thrust for knowledge
(5 - 1)
Client Side Scripting Language 5-2 Regular Expression, Rollover and
Frames
Output
(2) This string is then passed to the function TestString ab The string ab
function. This section is written in the <head> a|b a or b
section.
a* 0 or more a's
(3) In this function the regular expression is written as
\ Escapes a special character
re=/[az]/;
[ab-d] One character of: a, b, c, d
The regular expression begins and ends with slash /.
(4) a pair of square brackets [ ] appears following the [^ab-d] One character except: a, b, c, d
first slash. This tells the browser to search the text [\b] Backspace character
for characters that appear within the brackets. In this \d One digit
expression, two characters are within the square
\D One non-digit
brackets : ‘a’ and ‘z’ , which tells the browser to
determine whether the text includes a or z, or both. \s One whitespace
That's what is the regular expression! \S One non-whitespace
(5) This regular expression is assigned to the variable
\w One word character
re.
\W One non-word character
(6) Using test() method we can search for desired
characters from the input string input_str. * 0 or more
+ 1 or more
5.1.1 Language of Regular Expression
The words of regular expression are called special ? 0 or 1
5.1.3 Entering a Range of Characters match for ‘cot’ by using regular expression
For matching any digit we need not have to enter every /\bcot\b/
digit right from 0 to 9. Similarly for matching letters we 5.1.7 Replacing a Text using Regular Expressions
need not have to test with every single
alphabet. We can achieve this by entering range of Using replace function we can replace the desired
characters. pattern. The first parameter in the replace function is
For example – to match a digit we must have a regular the string which is to be replaced and the second
expression as [0-9]. Thus placing the range within a parameter is the replacing string.
square bracket helps us to evaluate a complete range of For example- Consider following JavaScript in which
set of characters. the word ‘country’ is replaced by ‘India’
<!DOCTYPE html>
Suppose we enter [j-s] then that means match the
<html>
characters j, k, l, m, n, o, p, q, r and s. <head>
<script type="text/javascript">
5.1.4 Matching Digits and Non Digits
function TestString(str)
Determining whether the string contains digits or non {
digits is a common task in any search pattern. For new_str=str.replace("country","India");
document.write(new_str);
instance – in the application of validating telephone
}
number this is the most wanted task. </script>
This can be simplified by JavaScript by writing the </head>
regular expression. <body>
<script type="text/javascript">
If we write \d then that means search the text for str="I love my country";
digits and if we write \D then that means search the document.write(str);
text for non-digits. </script>
Example Program
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function TestString(str1,str2)
{
re=/India/g;
result1=re.exec(str1);
result2=re.exec(str2);
if(result1)
document.write("<br/>The First text contains the word "+result1);
else
document.write("<br/>The First text does not contain the word 'India' ");
if(result2)
document.write("<br/>The Second text contains the word "+result2);
else
document.write("<br/>The Second text does not contain the word 'India' ");
}
</script>
</head>
<body>
<script type="text/javascript">
str1="I love my country";
str2="India has rich heritage and culture";
document.write(str1);
Client Side Scripting Language 5-6 Regular Expression, Rollover and
Frames
document.write("<br/>"+str2);
</script>
<form name="form1">
<input type="button" value="Check for Match" onclick="TestString(str1,str2)">
</form>
</body>
</html>
Output
$* Same as multiline
$& Same as lastMatch
$+ Same as lastParen
$` Same as leftContent
$' Same as rightContext
global Search globally (g modifier in use)
ignoreCase Search case-insensitive (i modifier in use)
input The string to search if no string is passed
lastIndex The index at which to start the next match
lastMatch The last match characters
lastParen The last parenthesized substring match
leftContext The substring to the left of the most recent match
multiline Whether strings are searched across multiple lines
prototype Allows the addition of properties to all objects
rightContext The substring to the right of the most recent match
source The regular expression pattern itself
Client Side Scripting Language 5-7 Regular Expression, Rollover and
Frames
Example Program
<!DOCTYPE html>
<html>
<head>
<title>Pattern Matching using Regular Expression </title>
<script type="text/javascript">
function TestString(str)
{
// for each Sunil - replace it with Mr. and then Sunil
alert(str.replace(/Sunil/g, 'Mr.$&'));
}
</script>
</head>
<body>
<script type="text/javascript">
str= "Sunil kumar, Sunil Shetty,Sunil Kale";
document.write(str);
</script>
<button onclick="TestString(str)">Change</button>
</body>
</html>
Output
Ex. 5.1.3 : Develop and demonstrate, using Javascript document that collects USN(the valid format is: A digit
from 1 to 4 followed by two upper-case characters followed by two digits followed by two upper case characters
followed by three digits:no embedded spaces allowed) of the user.Event handler must be included for the form
element that collects this information to validate the input. Message in the alert windows must be produced when
errors are detected.
Client Side Scripting Language 5-8 Regular Expression, Rollover and
Frames
Sol. :
<!DOCTYPE html>
<html>
<head>
<title>Pattern Matching using Regular Expression </title>
<script type="text/javascript">
function TestString(str)
{
document.write("The given string is: ");
document.write("<em>"+str+"</em>"+"<br/>");
var i=str.match(/[1234][A-Z]{2}\d{2}[A-Z]{2}\d{3}/);
if(i==null)
return false;
else
return true;
}
</script>
</head>
<body>
<h3>
<script type="text/javascript">
var input_str=prompt("Enter Your University Seat number here","");
if(TestString(input_str))
document.write("valid Seat Number");
else
document.write("Invalid Seat Number");
</script>
</h3>
</body>
</html>
Output
Client Side Scripting Language 5-9 Regular Expression, Rollover and
Frames
5.2 Frames Using frameset we can divide the rows and columns in
HTML frames allow us to present documents in
any number of frames. For example
<frameset rows = “20%,30%,50%” cols = “30%,*”>
multiple views.
This will create a frames in the browser’s window as
Using multiple views we can keep certain
follows -
information visible and at the same time other
views are scrolled or replaced.
For example, within the same window, one frame
can display a company information, a second frame
can be a navigation menu and a third frame may
display selected document that can be scrolled
through or replaced by navigating in the second
frame.
Fig. 5.2.2 Frames
Various frames can be set in one browser window .
In every layout frame we can load the desired html
page by using frame src. For example :
<frame src="D:\\html_examples\\bulleted1.html"
name="Left_Vertical">
By this statement, we are loading the web page
bulleted1.html in the specific frame and the frame is
named as Left_Vertical.
FrameSet.html
The above HTML document, the <frameset> tag is used
<!DOCTYPE html> to define frameset. The col attribute is used to create the
<html> three column frames.
<frameset cols="25%,*,50%">
<frame src="frame1.html"> 5.2.2 Invisible Borders of Frame
<frame src="frame2.html"> The borders of the frames can be made invisible by
<frame src="frame3.html">
setting the attributes “frameborder” and “border” = 0.
</frameset>
</html>
For example –
Step 2 : Create frame1.html, frame2.html and frame3.html <!DOCTYPE html>
files as follows – <html>
<frameset cols="30%,70%">
Frame1.html
<framesrc="frame1.html"frameborder="0" border="0"/>
<!DOCTYPE html>
<framesrc="frame2.html"frameborder="0" border="0"/>
<html> </frameset>
<body> </html>
<h1> Frame 1 </h1>
Output
</body>
</html>
Frame2.html
<!DOCTYPE html>
<html>
<body>
<h1> Frame 2 </h1>
</body>
</html>
Frame3.html
<!DOCTYPE html>
<html>
<body>
<h1> Frame 3 </h1>
</body>
</html>
Client Side Scripting Language 5 - 11 Regular Expression, Rollover and
Frames
5.2.3 Calling a Child Window
The main window defining the frames in it is called parent window and each frame contains the child
windows. For example –
We can call one child window from another child window. Following is a JavaScript in which we are
calling the function defined in another child window from one window.
Example Program
Step 1 : Create parent window script in which two frames are defined –
Test.html
<!DOCTYPE html>
<html>
<frameset cols="30%,70%">
<frame src="frame1.html" name="LeftPage"/>
<frame src="frame2.html" name="RightPage"/>
</frameset>
</html>
Step 2 : Write the frame1.html file as a LeftPage frame. The code for this is as follows –
frame1.html
<!DOCTYPE html>
<html>
<body>
<h1> Frame 1 </h1>
<form>
<input type="button" name="Frame1" value ="Click Me" onclick="parent.RightPage.MyFunction()"/>
</form>
</body>
</html>
Client Side Scripting Language 5 - 12 Regular Expression, Rollover and
5.2.4 Frames and Focus of Child
Changing the Content
Step 3 : The definition of MyFunction is present in the Window
second frame. Hence the code for this is - You can change the content of a child window from a
Frame2.html JavaScript function by modifying the source web page
<!DOCTYPE html> for the child window.
<html>
We can assign the new source to the child window’s
<head>
<title>Frame 2</title> href attribute.
<script language="Javascript" Example Program : In the following example we are
type="text/javascript"> displaying two frames – Frame1 on the left hand side
function MyFunction() and Frame to at the right hand side. When the user
{ clicks the button present in the frame1, the frame2 is
document.write("Some Function is Called... "); removed and is replaced by Frame3.
}
Step 1 : We will write the JavaScript for displaying two
</script>
frames on the parent window –
</head>
<body>
MainWindow.html
<h1> Frame 2 </h1>
<!DOCTYPE html>
</body>
<html>
</html>
<frameset cols="30%,70%">
Step 3 : Now we will execute the parent JavaScript i.e. <frame src="frame1.html" name="LeftPage"/>
test.html on web browser <frame src="frame2.html" name="RightPage"/>
</frameset>
</html>
Step 2 : The frame1 contains one button componenet. The
code for this is as follows –
Frame1.html
<!DOCTYPE html>
<html>
<head>
<script language="Javascript" type="text/javascript">
function MyFunction()
{
parent.RightPage.location.href='frame3.html'
}
Just click the Click Me button and you can see the </script>
changes in another child window – </head>
<body>
<h1> Frame 1 </h1>
<form>
<input type="button" name="Frame1"
value ="Click Me" onclick="MyFunction()"/>
</form>
</body>
</html>
Step 2 : The code for frame2 is as follows –
Frame2.html
<!DOCTYPE html>
<html>
<head>
Client Side Scripting Language 5 - 13 Regular Expression, Rollover and
Frames
5.2.5 Accessing Elements of Another Child Window
<title>Frame 2</title>
</head> It is possible to change the elements of one frame from
<body> another frame. For example – we can change the label
<h1> Frame 2 </h1>
of ‘button’ component of right frame from one the left
</body>
parent.RightPage.form2.Frame2.value="Calculate"
}
</script>
</head>
<body>
<h1> Frame 1 </h1>
<form name="form1">
<input type="button" name="Frame1"
value ="Click Me" onclick="MyFunction()"/>
</form>
</body>
</html>
Client Side Scripting Language 5 - 14 Regular Expression, Rollover and
5.3 Rollover Frames
Step 3 : The second frame contains a single button
Rollover means change in the appearance of the object
element.
Frame2.html
when user moves his or her mouse over an object on
<!DOCTYPE html> the page.
<html>
Just click the Click Me button and see the change in onmouseout="src=' mickey1.jpg'">
</a>
second frame
</body>
</html>
Client Side Scripting Language 5 - 15 Regular Expression, Rollover and
Frames
Output
<a onmouseover="document.fruit.src='
5.3.2 Text Rollover MangoImg.jpg'">
Text rollover is a technique in which whenever user <b><u>Mango</u></b>
rollover the text , JavaScript allows to change the </a>
<br/><br/><br/><br/>
page element usually some graphics image.
<a onmouseover="document.fruit.src
Carefully placed rollovers can enhance a visitor's ='OrangeImg.jpg'">
experience when browsing the web page. <b><u>Orange</u></b>
</a>
Following example illustrates this idea
<br/><br/><br/><br/>
Ex. 5.3.1 : Write a JavaScript in which when user rolls <a onmouseover="document.fruit.src
over the name of fruit, the corresponding image should ='banana.jpg'">
be displayed. For instance – if user rolls over the text <b><u>Banana</u></b>
</a>
“Mango”; the image of Mango should be displayed.
</td>
JavaScript Document </tr>
</table>
<!DOCTYPE html>
</body>
<html>
</html>
<head>
<title>Rollover Text</title>
</head>
<body>
<table>
<tr>
<td>
<a>
<IMG src="MangoImg.jpg"
name="fruit">
</a>
</td><td>
Client Side Scripting Language 5 - 16 Regular Expression, Rollover and
Frames
Output
Clearly, the above output illustrates that when user rolls over the text Mango , the image of mango will
be displayed, if user rolls over the text Orange, the image of orange will be displayed and if user rolls
over the text Banana, the image of banana will be displayed.
Client Side Scripting Language 5 - 17 Regular Expression, Rollover and
Frames
5.3.3 Multiple Actions for Rollover
Suppose user is rolling the cursor over the text, then instead of simply changing the image we can
display more window displaying some features or additional information about the item on which the
mouse is rolling over. This process is referred as multiple actions for rollover.
Due to this effect visitor gets more information at a glance.
We can open additional window using the built in function Open(). This function is invoked using
the object Window.
The open() method opens a new browser window, or a new tab. The close() window closes the
window.
The window. open() function can be written as follows
This function returns the object or instance of a window. We store this instance in a variable named
MyWindow.
Then using MyWindow.document.write() function we can write the information to this opened
window. Thus it is possible to write additional information by opening and writing the contents to
additional window.
Following example illustrates this idea.
Ex. 5.3.2 : Write a JavaScript in which when user rolls over the name of fruit, the corresponding image should be
displayed. Along with the appropriate image display, additional window should pop up displaying the benefit of
each fruit.
Sol. :
<!DOCTYPE html>
<html>
<head>
<title>Rollover Text</title>
<script language="Javascript">
function MyFunction(choice)
{
if(choice==1)
{
document.fruit.src='MangoImg.jpg'
MyWindow=window.open('','infoWindow',
'height="20",width="20",left="30",top="30"')
MyWindow.document.write(
'Great source of Vitamin A and Vitamin C supplement')
}
if(choice==2)
{
document.fruit.src='OrangeImg.jpg'
Client Side Scripting Language 5 - 18 Regular Expression, Rollover and
Frames
MyWindow=window.open('','infoWindow',
'height="20",width="20",left="30",top="40"');
MyWindow.document.write(
'Great source of Vitamin C supplement and rich antioxidant')
}
if(choice==3)
{
document.fruit.src='banana.jpg'
MyWindow=window.open('','infoWindow',
'height="20",width="20",left="30",top="50"');
MyWindow.document.write(
'Full of Potassium and Fiber')
}
}
</script>
</head>
<body>
<table>
<tr>
<td>
<a>
<IMG src="MangoImg.jpg" name="fruit">
</a>
</td>
<td>
<a onmouseover="MyFunction(1)"
onmouseout="MyWindow.close();">
<b><u>Mango</u></b>
</a>
<br/><br/><br/><br/>
<a onmouseover="MyFunction(2)"
onmouseout="MyWindow.close();">
<b><u>Orange</u></b>
</a>
<br/><br/><br/><br/>
<a onmouseover="MyFunction(3)"
onmouseout="MyWindow.close();">
<b><u>Banana</u></b>
</a>
</td>
</tr>
</table>
</body>
</html>
Client Side Scripting Language 5 - 19 Regular Expression, Rollover and
Frames
Rollimage[1]=new Image(100,100)
Rollimage[1].src='OrangeImg.jpg'
Rollimage[2]=new Image(100,100)
Rollimage[2].src='banana.jpg'
</script>
</head>
Client Side Scripting Language 5 - 20 Regular Expression, Rollover and
Frames
<body>
<table>
<tr>
<td>
<a>
<IMG src='MangoImg.jpg' name="fruit">
</a>
</td>
<td>
<a onmouseover="document.fruit.src=Rollimage[0].src">
<b><u>Mango</u></b>
</a>
<br/><br/><br/><br/>
<a onmouseover="document.fruit.src=Rollimage[1].src">
<b><u>Orange</u></b>
</a>
<br/><br/><br/><br/>
<a onmouseover="document.fruit.src=Rollimage[2].src">
<b><u>Banana</u></b>
</a>
</td>
</tr>
</table>
</body>
</html>
Output
It is same as in example 5.3.1.