css unit 5
css unit 5
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Regular Expression:
-->
- A regular expressionn is a sequence of characters that define a search pattern.
- It is mainly used for string pattern matching.
- Regular expressions are very powerful and can be used to perform a wide variety
of string manipulations.
- It is usually used to validate data entered in forms.
- It is also called as regex.
Regular Expressions can be created in two ways:
i] Using regex literal
Syntax:
let regex = /pattern/flags
Ex:
let regex = /[a-z]/g;
Ex:
<!DOCTYPE html>
<html>
<head>
<title>Regular Expression Ex</title>
</head>
<body>
<script>
let r1=/[a-z]/g;
let r2=new RegExp("[0-9]","g");
</script>
</body>
</html>
<script>
let str = "Hello123";
let result1 = str.match(r1);
let result2 = str.match(r2);
document.write("Alphabets: ", result1);
document.write("<br>Numbers: ", result2);
</script>
Quantifiers
-->
Quantifiers provide way to specify how many times a particular character or set of
characters is allowed to repeat itself within a pattern.
Each special character has a specific connotation.
Ex:
<!DOCTYPE html>
<html>
<head>
<title>Quantifiers</title>
</head>
<body>
<script>
var a,b,c,d,e,f;
a=/\d{4}/;
b=/\d{4,5}/;
c=/\d{4,}/;
d=/\d*/;
e=/\d+/;
f=/\d?/;
</script>
</body>
</html>
Meta Characters
-->
- Special characters that define the structure of the search pattern.
- A metacharacter is an alphabetical character preceded by a backslash that acts to
give the combination a special meaning.
Character & Description
. (dot) a single character
\s a whitespace character (space, tab, newline)
\S non-whitespace character
\d a digit (0-9)
\D a non-digit
\w a word character (a-z, A-Z, 0-9, _)
\W a non-word character
[\b] a literal backspace (special case).
[aeiou] matches a single character in the given set
[^aeiou] matches a single character outside the given set
(foo|bar|baz) matches any of the alternatives specified.
Modifiers/Flags
-->
Modifiers: Options to alter the behavior of the regular expression.
Modifier Description
g: Perform a global match (find all matches rather than stopping after the
first match)
i: Perform case-insensitive matching
m: Perform multiline matching
test() method:
-->
It tests for a match in a string.
This method returns true if it finds a match, otherwise it returns false.
Syntax:
regexobj.test(string);
regexobj: Object of regular expression.
string: Pattern to search.
Ex:
<!DOCTYPE html>
<html>
<head>
<title>test() method</title>
</head>
<body>
<script>
function sear(){
var a=/\d{4}/;
if(a.test("1234")){
document.write("Pattern is found.");
}else{
document.write("Pattern is not found.");
}
}
sear();
</script>
</body>
</html>
search() method:
-->
This method is used to search a specified pattern in the string.
It returns the index where match is found.
Syntax:
str.search(pattern);
str: String where pattern will be searched.
pattern: Pattern to search.
Ex:
<!DOCTYPE html>
<html>
<head>
<title>search() method</title>
</head>
<body>
<script>
function sear(){
var a=/\d{4}/;
b="1234".search(a);
if(b!=-1){
document.write("Pattern is found.");
}else{
document.write("Pattern is not found.");
}
}
sear();
</script>
</body>
</html>
match() method.
-->
It searches a string for a specified pattern.
It returns match word as an array.
It returns null if no match is found.
Syntax:
str.match(regex);
str: String where pattern will be searched.
regex: Pattern to search.
Ex:
<!DOCTYPE html>
<html>
<head>
<title>match() method</title>
</head>
<body>
<script>
function sear(){
var a=/\d{4}/;
b="this is 1234".match(a);
document.write("Match found : "+b);
}
sear();
</script>
</body>
</html>
replace() method.
-->
It searches a string for a specified pattern, if found replaces matched substring
with replacement substring.
Syntax:
str.replace(regex,rstr);
str: String where pattern will be searched.
regex: Pattern to search.
rstr: Replacement String.
Ex:
<!DOCTYPE html>
<html>
<head>
<title>replace() method</title>
</head>
<body>
<script>
var a=/\d{4}/;
str='this is 1234';
b=str.replace(a,'5678');
document.write("Original string : "+str+"<br>");
document.write("New string : "+b);
</script>
</body>
</html>
<html>
<head>
<title>JavaScript RegExp ignoreCase Property</title>
</head>
<body>
<script type = "text/javascript">
var re = new RegExp( "string" );
if ( re.ignoreCase ){
document.write("Test1-ignoreCase property is set");
} else {
document.write("Test1-ignoreCase property is not set");
}
re = new RegExp( "string", "i" );
if ( re.ignoreCase ){
document.write("<br/>Test2-ignoreCase property is set");
} else {
document.write("<br/>Test2-ignoreCase property is not set");
}
</script>
</body>
</html>
Frame
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Frame
-->
A frame is an HTML element that allows the display of another HTML document within
the current web page. Frames are implemented using the <frameset>, <frame> and
<iframe> element in HTML.
Frames are used to divide browser window into multiple sections where each section
is treated as window that can have independent contents. A frame can load separate
HTML document in each frame in a window.
<frame> tag: Frame tag is used to insert web page content in a frame. It is an
empty tag.
Attributes:
src = “URL” Specify address of a web page to be displayed in a frame.
name = “ string” Specify name of the frame which can be used as target to open a
link.
noresize Specify that the frame is not resizable.
frameborder Specifies whether borders of frame are shown.It takes values 1 or
0.
Ex:
page1.html
<!DOCTYPE html>
<html>
<body>
<h1> Frame 1 </h1>
</body>
</html>
page2.html
<!DOCTYPE html>
<html>
<body>
<h1> Frame 2 </h1>
</body>
</html>
final.html
<html>
<frameset cols = “30%,70%” border="5" bordercolor="black">
<frame src = “page1.html” name=”f1”>
<frame src = “page2.html” name=”f2”>
</frameset>
</html>
child1.html
<!DOCTYPE html>
<html>
<head>
<title>Child 1</title>
</head>
<body>
<center>
<h1>Child 1</h1>
<input type="button" value="Click Here" onclick="parent.n2.show()">
</center>
</html>
child2.html
<!DOCTYPE html>
<html>
<head>
<title>Child 2</title>
</head>
<body>
<script>
function show(){
document.write("How aer you my friend?");
}
</script>
<center>
<h1>Child 2</h1>
</center>
</body>
</html>
parentframe.html
<!DOCTYPE html>
<html>
<head>
<title>Parent Frame</title>
</head>
<frameset cols="25%,*">
<frame src="child1.html" name="n1">
<frame src="child2.html" name="n2">
</frameset>
</html>
child1.html
<!DOCTYPE html>
<html>
<head>
<title>Child 1</title>
</head>
<body>
<center>
<h1>Child 1</h1>
<input type="button" value="Click Here"
onclick="parent.n2.location.href='child3.html'">
</center>
</html>
child2.html
<!DOCTYPE html>
<html>
<head>
<title>Child 2</title>
</head>
<body>
<center>
<h1>Child 2</h1>
</center>
</body>
</html>
child3.html
<!DOCTYPE html>
<html>
<head>
<title>Child3</title>
</head>
<body>
<center>
<h1>Child 3</h1>
<h3>Hello Chalo, HEllo GO</h3>
</center>
</html>
parentex.html
<!DOCTYPE html>
<html>
<head>
<title>Parent Frame</title>
</head>
<frameset cols="25%,*">
<frame src="child1.html" name="n1">
<frame src="child2.html" name="n2">
</frameset>
</html>
Ex:
child1.html
<!DOCTYPE html>
<html>
<head>
<title>Child 1</title>
</head>
<body>
<center>
<h1>Child 1</h1>
</center>
</html>
child2.html
<!DOCTYPE html>
<html>
<head>
<title>Child 2</title>
</head>
<body>
<center>
<h1>Child 2</h1>
<input type="button" value="Click Here" onclick="parent.n1.focus()">
</center>
</body>
</html>
parentframe.html
<!DOCTYPE html>
<html>
<head>
<title>Parent Frame</title>
</head>
<frameset cols="25%,*">
<frame src="child1.html" name="n1">
<frame src="child2.html" name="n2">
</frameset>
</html>
Ex:
<html>
<head>
<script>
function ChangeContent()
{
window.t1.document.open();
window.t1.document.write('<html >');
window.t1.document.write('<body>');
window.t1.document.write('<h1>Parent Frame</h1>');
window.t1.document.write('</body>');
window.t1.document.write('</html>');
window.t1.document.close();
}
</script>
</head>
<frameset rows="50%,50%" onload="ChangeContent()">
<frame src=" " name="t1" />
<frame src="webpage2.html" name="t2" />
</frameset>
</html>
p1.html
<!DOCTYPE html>
<html>
<head>
<title>Frame 1</title>
</head>
<body>
<form name="f1">
<input type="text" name="r1">
<input type="button" name="b1" value="button1" onclick="change1()">
</form>
<script>
function change1() {
parent.n2.document.f2.b2.value = "ab";
}
</script>
</body>
</html>
p2.html
<!DOCTYPE html>
<html>
<head>
<title>Frame 2</title>
</head>
<body>
<form name="f2">
<input type="text" name="r2">
<input type="button" name="b2" onclick="change1()" value="button2">
</form>
<script>
function change1() {
parent.n1.document.f1.b1.value = "ac";
}
</script>
</body>
</html>
Rollover
-----------------------------------------------------------------------------------
-------------------------------------------------------------
Rollover/ Creating Rollover
-->
Rollover means change in the appereance of object when user his mouse over an
object on the page.
The rollover effect is mainly used in webpage designing for advertising purpose.
There are two ways to create rollover, using plain html or mixture of html and
javascript.
Javascript rollovers are handled by adding an onmouseover or onmouseout event.
- onmouseover is triggered when mouse moves over an element.
- onmouseout is triggered when mouse moves away from the element.
Ex:
<!DOCTYPE html>
<html>
<head>
<title>Rollover Ex</title>
</head>
<body>
<img src="m.png" width="300px" height="400px" onmouseover="src='m1.png'"
onmouseout="src='m2.png'">
</body>
</html>
TextRollover
-->
Text rollover is a technique whenever, user rollovers text, javascript allows to
change the page element usually some graphics image.
Ex:
<html>
<head></head>
<body>
<textarea
cols="50"
name="rollovertext"
onmouseover="this.value='What is rollover?'"
onmouseout="this.value='Rollover means a webpage changes when the user moves
his or her mouse over an object on the page'"
></textarea>
</body>
</html>
Ex:
<html>
<head>
<title>text rollovers</title>
<script>
function open_new_window(clrname) {
if (clrname == 1) {
document.clr.src = "red.png";
mwin = window.open(
"",
"myadwin",
"height=100,width=150,left=500,top=200"
);
mwin.document.write("looks like red color");
}
if (clrname == 2) {
document.clr.src = "green.png";
mwin = window.open(
"",
"myadwin",
"height=100,width=150,left=500,top=200"
);
mwin.document.write("looks like green color");
}
if (clrname == 3) {
document.clr.src = "blue.png";
mwin = window.open(
"",
"myadwin",
"height=100,width=150,left=500,top=200"
);
mwin.document.write("looks like blue color");
}
}
</script>
</head>
<body>
<table border="1" width="100%">
<tbody>
<tr valign="top">
<td width="50%">
<a><img height="500" src="red.png" width="900" name="clr" /></a>
</td>
<td>
<h2>
<a onmouseover="open_new_window(1)" onmouseout="mwin.close()">
<b><u>RED</u></b></a
>
<br /><br />
<a onmouseover="open_new_window(2)" onmouseout="mwin.close()">
<b><u>GREEN</u></b></a
>
<br /><br />
<a onmouseover="open_new_window(3)" onmouseout="mwin.close()">
<b><u>BLUE</u></b></a
>
</h2>
</td>
</tr>
</tbody>
</table>
</body>
</html>