CSS Unit2 Notes
CSS Unit2 Notes
2.1 Array
An array can comprise one or multiple elements of
same type.
Each element is like a variable in that an array
element refers to a memory location where
information can be temporarily stored.
An array is identifi edby a unique name, similar to
the name of a variable.
A number called an index identifies an array element.
The combination of the array name and an index is
nearly the same as a variable name.
In your JavaScript, you use both the array name and
the index to specify a particular memory location.
Declaring an Array
You create an array by writing a declaration
statement in your JavaScript, which is very similar to
the way you declared a variable. This declaration
statement has five parts:
The first part is the var keyword;
The second part is the array name, which you create;
The third part is the assignment operator(=);
The fourth part is the new operator;
and the fifth part is the Array() constructor.
All these parts are shown here:
Syntax:
var Array_name = new Array()
e.g.
var products = new Array()
Initializing an Array
Initialization is the process of assigning a value when
either a variable or an array is declared.
When initializing an array, you place the value within
the parentheses of the Array() constructor.
Syntax:
var Array_name=new Array(Value1,Value2,.......Valuen)
e.g.
var products = new Array('Burger', 'Water', 'Pizza', 'Bread')
Initializing an Array
Notice the following:
Each value is placed within the parentheses of
the Array() constructor.
Values must be the same type of information.
This can be a string, number, Boolean, and
object types.
The preceding code segment uses strings ('Burger',
'Water', 'Pizza', 'Bread').
JavaScript won’t let us use a mixture of types; all of
the values must be the same type.
A comma must separate each value.
var PartialList=fruits.slice(2,5)
for (var i = 0;i<PartialPhoneList.length;i++)
{
document.write(PartialList[i] + '<br>')
}
</script>
</body>
</html>
2.2 Function
A function performs an action when you call the
function in a JavaScript—such as when you called the
alert() function to display a message on the screen.
Two kinds of functions are used in JavaScript:
predefined functions and custom functions.
A predefined function is already created for you in
JavaScript, such as the alert() function.
A custom function is a function that you create.
Defining a Function
• A function must be defined before it can be called
in a JavaScript statement.
• If you think about it, this makes sense, because the
browser must learn the definition of the word (the
function name) before the browser sees the word
(the function call) in a statement.
• The best place to define a function is at the
beginning of a JavaScript that is inserted in the
<head> tag, because then all subsequent
JavaScripts on the web page will know the
definition of that function.
• The browser always loads everything in the <head>
tag before it starts executing any JavaScript.
A function definition consists of four parts: the name,
parentheses, a code block, and an optional return
keyword.
• Function Name
• The function name is the name that you’ve
assigned the function. It is placed at the top of the
function definition and to the left of the
parentheses.
• Any name will do, as long as it follows certain
naming rules. The name must be
• • Letter(s), digit(s), or underscore character
• • Unique to JavaScripts on your web page, as no
two functions can have the same name.
• The name cannot
• • Begin with a digit.
• • Be a keyword.
• • Be a reserved word.
• The name should be
• • Representative of the action taken by statements
within the function.
• Parentheses
• Parentheses are placed to the right of the function
name at the top of the function definition.
• Parentheses are used to pass values to the function;
these values are called arguments.
• Code Block
• The code block is the part of the function defi
nition where you insert JavaScript statements that
are executed when the function is called by
another part of your JavaScript application.
• Open and close French braces define the
boundaries of the
• code block.
• Statements that you want executed must appear
between the open and close French braces.
• Return (Optional)
• The return keyword tells the browser to return a
value from the function definition to the statement
that called the function.
• Not all functions return a value.
• For example, a function that displays a message on
the screen doesn’t need to return a value to the
statement that calls the function.
• Therefore, the return keyword doesn’t need to be
included in the function definition.
• e.g.
function IncreaseSalary()
{
var salary = 500000 * 1.25
alert("Your new salary is " + salary)
}
Adding Arguments
• A function typically needs data to perform its task.
• Sometimes you provide the data when you define
the function, such as the salary and percentage
increase in salary in the preceding example.
• Other times, the data is known only when you run
your JavaScript.
• An argument is one or more variables that are
declared within the parentheses of a function
definition.
• This is illustrated in the following code sample.
• OldSalary is an argument of the IncreaseSalary()
function.
function IncreaseSalary(OldSalary)
{
var NewSalary = OldSalary * 1.25
alert("Your new salary is " + NewSalary)
}
• The JavaScript statement or the HTML code
provides the value when it calls the function. This is
called passing a value to the function.
• Think of an argument as a variable, which you
learned about .
• You assign a name to an argument following the
same rules that apply to naming a variable.
• Anything you can do with a variable you can do
with an argument.
• You might be wondering how an argument is
assigned a value. This happens when the function
is called either by a statement within the JavaScript
or by HTML code on your web page.
Adding multiple Arguments
• You can use as many arguments as necessary for
the function to carry out its task.
• Each argument must have a unique name, and
each argument within the parentheses must be
separated by a comma.
• e.g.
function IncreaseSalary(OldSalary, PercIncrease)
{
var NewSalary = OldSalary * (1 + (PercIncrease / 100))
alert("Your new salary is " + NewSalary)
}
Calling a Function
• A function is called by using the function name
followed by parentheses.
• If the function has arguments, values for each
argument are placed within the parentheses.
• You must place these values in the same order that
the arguments are listed in the function definition.
• A comma must separate each value.
• Here’s how the IncreaseSalary() function is called:
• e.g.
IncreaseSalary(500000, 6)
IncreaseSalary(Salary, Increase)
-->
</script>
</body>
</html>
Calling a Function from HTML
• A function can be called from HTML code on your
web page. Typically, a function will be called in
response to an event, such as when the web page
is loaded or unloaded by the browser.
• You call the function from HTML code nearly the
same way as the function is called from within a
JavaScript, except in HTML code you assign the
function call as a value of an HTML tag attribute.
Let’s say that you want to call a function when the
browser loads the web page. Here’s what you’d
write in the <body> tag of the web page:
<body onload = "WelcomeMessage()">
• Here’s what you’d write to call the function right
before the user moves on to another web page:
<body onunload = "GoodbyeMessage()">
function GoodbyeMessage()
{
alert('So long.')
}
-->
</script>
</head>
<body onload="WelcomeMessage()" onunload="GoodbyeMessage()">
</body>
</html>
Creating a Popup Window
• Popup windows are probably the most annoying
things on the Internet.
• You surf to a web site only to be shown a popup ad,
and then when you leave the site you’re shown
another popup ad.
• Nevertheless, popups can be a necessary evil when
you’re creating web sites.
• You can create a function that displays a popup
window that you design on the fly.
• The new window is opened by calling the
window.open() method of the window object.
e.g.
<!DOCTYPE html >
<html>
<head>
<title>Calling a function from HTML</title>
<script language="Javascript" type="text/javascript">
<!--
function WelcomePopup()
{
window.open();
alert('Glad to see you.')
}
function GoodbyePopup()
{
window.open();
alert('So long.')
}
-->
</script>
</head>
<body onload="WelcomePopup()" onunload="GoodbyePopup()">
</body>
</html>
2.4 String
• A string is a series of characters that form text.
• It is often necessary to take apart and rearrange
text so that the information can be processed
properly. This is referred to as manipulating a
string
Why Manipulate a String?
• Text, to most of us, is a series of words, such as
Bob Smith, which is a customer name.
• However, commercial applications don’t use text.
• Instead, they use data, which is defined as the
smallest available amount of meaningful
information.
• 'Bob Smith' is a text string, since the words are
enclosed in quotation marks.
• You can recognize this string as a person’s name.
• The person’s name can be divided into two pieces
of data, commonly referred to as data elements.
• These are first name and last name, which are the
smallest amount of meaningful information in the
text string of a person’s name.
• Text must be transformed into data elements if
information gathered by your application is to be
stored in a database, which is like an electronic
filing cabinet for pieces of data.
• Commercial applications store a person’s first
name('Bob')
• and last name('Smith') separately, rather than the
full name ('Bob Smith') in the database.
• This means you must write statements that divide
'Bob Smith' into 'Bob' and 'Smith'. This is called
manipulating a string.
Joining String
(string manipulation by concatenating two strings)
Joining String:Example 2
<!DOCTYPE html>
<html>
<head>
<title>Concatenating a string</title>
</head>
<body>
<script language="Javascript" type="text/javascript">
<!--
var FirstName = 'Bob'
var LastName = 'Smith'
var space = ' '
var newString = FirstName + space + LastName
alert(newString)
-->
</script>
</body>
</html>
Retrieving a character from given position
• Each character in a string is an array element that
can be identified by its index.
• var FirstName = 'Bob'
• You can copy a character from a string to another
string by using the charAt() method of the string
object.
• The charAt() method requires one argument, which
is the index of the character that you want to copy.
• The following statement illustrates how to use the
charAt() method:
• var SingleCharacter=NameOfStringObject.charAt(index)
Dividing Text
• Imagine a string of concatenated data elements
with only a comma separating each of them; your
mission is to copy each data element into its own
string.
• Here, each person’s name is a data
ele_x0002_ment, and your job is to copy each
name to its own string:
• var DataString = 'Bob Smith, Mary Jones, Tom
Roberts, Sue Baker'
• JavaScript developers call this a comma-delimited
string because a comma signifies the beginning and
end of each data element.
• Traditionally, data elements are transferred
between applications in a comma-delimited format.
• The application receiving the string then uses the
commas as a guide for separating the string into
data elements.
• Your task is challenging, but it can easily be
accomplished by using the split() method of the
string object.
• The split() method creates a new array and then
copies portions of the string, called a substring,
into its array elements.
• You must tell the split() method what string
(delimiter) is used to separate substrings in the
string.
• You do this by passing the string as an argument to
the split() method.
• In this example, the comma is the string that
separates substrings. Here’s how you use the split()
method:
• var NewStringArray = StringName.split('delimiter
string')
• Example
<!DOCTYPE html>
<html>
<head>
<title>Dividing a delimited string into a
substring</title>
</head>
<body>
<script language="Javascript" type="text/javascript">
<!--
var DataString = 'Bob Smith,Mary Jones,Tom
Roberts,Sue Baker'
var NewArray = DataString.split(',')
document.write(DataString);
document.write('<br>')
for (i=0; i<4; i++)
{
document.write(NewArray[i])
document.write('<br>')
}
-->
</script>
</body>
</html>
Copying a Substring
• You if you need to copy one substring, you’ll need
to use one of two other methods: substring() and
substr().
• Here’s the string that the sales representative
entered that contains the e-mail address:
• EmailAddress = '[email protected] '
• There is a good chance that the www.xyz.com is
the corporate web site for this client.
• Your job is to copy the substring 'xyz.com' from the
e-mail address and then concatenate the substring
with 'www.' to form the new string 'www.xyz.com'.
• substring() is a method of a string object that
copies a substring from a string based on a
beginning and an end position that is passed as an
argument to the substring() method.
regex = /Dog/i;
var str2=str.replace(regex, 'ferret')
expected output: "The quick brown fox jumps over the
lazy ferret. If the dog reacted, was it really lazy?"