LAB5 Javascript
LAB5 Javascript
LAB MANUAL 5
Javascript
LEARNING OUTCOMES:
• Learn the fundamentals of implementing and distributing a JavaScript.
• Learn to use the JavaScript.
• Learn the fundamentals of implementing and distributing advanced
JavaScript.
• Learn to use the advanced JavaScript.
VARIABLE
A variable's value can change during the script. You can refer to a variable
by name to see its value or to change its value.
FUNCTION
JavaScript
Please self-study XHTML and javascript in
http://www.w3schools.com
<html>
<head>
<META http-equiv="Content-Type"
content="text/html;
charset=ISO-8859-5">
<title>First JavaScript example</title>
<script type = 'text/javascript'>
alert("This is an alert box!")
</script>
</head>
<body>
<H1>First JavaScript Example</H1>
<P>Theoretically, an alert box should
have popped up
before you see this text.</P>
</body>
</html>
The text in shaded background is new. The alert function pops up a dialog box.
You must click OK before you can see the page content.
JavaScript Errors
Let's create an error by removing the second quote mark in the alert
statement:
alert("This is an alert box!)
Save the file and use the Firefox File menu to open the saved version. You
should see the page content telling you about the alert box, but no alert
box popped up. The error caused the script to silently fail!
Since we are developers, we want to know why the script failed. From the
Firefox Tools menu choose Error Console. Click on Errors and you should
see the helpful message below.
</body>
</html>
You will see that this element has three attributes: its type (button),
value ('Click here') and the onclick event handler, show_alert.
<H1>Rollover Example</H1>
<P onmouseover='show_alert()'>Roll the mouse
over this paragraph to see the rollover
effect.</P>
function test_getElement() {
id = prompt("Please enter the id of a page
element");
obj = document.getElementById(id);
if(obj)
alert("Found element; its tag name is
"+obj.tagName);
else
alert("Sorry, I cannot find this element");
}
</script>
</head>
<body id='Suzie'>
<H1 id='Steve'>Object Reference Tester</H1>
<P id='Alice'>
Click the button to test the getElementById
function.</P>
<div id='Fred'>
<input type='button' value='Click here'
onclick="test_getElement()">
Again, there are lots of pitfalls with copying this code, so use the Error
Console to verify that you have it correct. Here are some of the new
features in the code:
• The prompt function is like alert, but it makes the user enter
some value that is returned to the script. The value the user types
in will be called id.
• We added id attributes to various tags in the document. These
are special markers to identify individual elements (just like your
student ID identifies you). You should not use the same id in two
different tags.
• The document.getElementById function tries to find the
page element that has a given id. If successful, the function
creates an object reference and stores it in the variable obj.
• The script needs to make one
of two different responses to
the user. If the user enters an
id that is found in the
document, the script will
reply with the tag name (P,
H1, etc.). But if the user
enters an id that is not found,
the script must give a 'not
found' message. To handle
this, the script uses an if-else
control statement. The flow chart for the script logic is shown to
the right.
}
</script>
</head>
<body id='Suzie'>
<H1 id='Steve'>Object Reference Tester</H1>
<P id='Alice'>
Click the button to alter the style of this
paragraph.</P>
<div id='Fred'>
<input type='button' value='Change to blue'
onclick="change_style()">
When the script changes the className property of the element, the
display property will change! Be sure to capitalize the 'N' in className,
because JavaScript is a case-sensitive language.
Object Terminology
Note the special usage obj.className='highlight'.
Here obj is an object: a package containing a
bunch of data items that can be manipulated by a
program. One of the data items is className, the
name of the style used to display the paragraph
contents. By changing className, we have changed
the way the paragraph displays in the browser.
Function Parameter
We can make functions more flexible by providing parameters. These are
input values that give the function some details about how to do its job.
Make the highlighted code changes and save the file as
myJavascript7.html:
obj =
document.getElementById(theElement);
obj.className=theStyle;
}
...
...
...
<input type='button' value='Change to blue'
onclick="change_style('Alice','highlight')">
Note that the function spec is now a bit more complex: this function can
change any element to any style you like, not just 'highlight'. Be careful
with the last line: it contains the single-quoted Alice and highlight inside
the double-quoted change_style().
This code should have exactly the same effect as myJavascript6.html. We
won't see the payoff from our latest improvements until we need to
change styles more than once.
function change_weight() {
paragraph_obj =
document.getElementById("para1");
paragraph_obj. className='bold';
}
</script>
</head>
<body>
<H1>Text object properties</H1>
1. Create a theatre booking form html page. (Like the one below)
2. Refer to the lecture notes and write the code that will show an alert
box with a welcome message when the page is loaded. (Hint: you
will need to set the onload event in the Body tag).
Then add the code which will call this function to the form tag as follows:
<form method="post" onsubmit="return checkall(this);"
action="movie.html">
4. Test the code to make sure that the validation works like this:
5. Amend the code so that it also does the existence validation for both
the email and phone text boxes additionally.
<html>
<head>
<title>My Bestie</title>
</head>
<body>
<p> Click Refresh (or Reload) to run the script
again. </p>
</body>
</html>
2. Please type the below code and add three more questions and answers
based on chapter
<html>
<head>
<title>My Bestie</title>
<script type="text/javascript">
var firstAnswer;
var secondAnswer;
</script>
</head>
<body>
<p> Click Refresh (or Reload) to run the script
again.</p>
</body>
</html>