Академический Документы
Профессиональный Документы
Культура Документы
Lecture 15
More on Interactive Forms (Web Development Lecture 5)
1
In Todays Lecture
We will learn ways of adding more interactivity to forms We will get our first taste of JavaScript the object-based language that we will be employing throughout the rest of the Web development part of this course
Last time we mentioned server-side scripts; today we will write (simple) client-side scripts in JavaScript 3
But first lets review the tags that are used in forms
Hidden Input
12
13
</SELECT>
16
17
18
19
20
Lets now review what happens when I enter all the required info and press the Send eMail button?
21
Browser
Server-Side Script
A Reasonable Scenario
When the Send eMail button is clicked, the browser sends the data collected through the form to a script running on the Web server That server-side script:
receives that data analyzes it discovers the missing or incorrect data sends a message back to the users browser stating the problem and asks the user to re-send
24
This process
That is the process of user:
Filling the incomplete/incorrect data Sending it to the server Receiving the response back from the server Correcting and resending
is quite time-consuming and uses the servers resources to help the user correct his mistakes It can really bog down the server if a large number of users are using that Web server 25
Well use JavaScript to do client-side scripting. However, there are many other languages that can be used for that purpose, e.g. VBScript 28
Reduced network traffic as the forms data is sent only once instead of many tos and fros
29
Disadvantages
Client-side scripts do not work with all browsers Some user intentionally turn scripting off on their browsers This increases the complexity of the Web page, as it now has to support both situations: browsers with scripting capability, and those not having that capability
30
31
32
Code for the simple Send eMail button as was described during the last Web development lecture
33
<INPUT type=submit name=sendEmail value=Send eMail onMouseOver= if (document.sendEmail.sender.value.length < 1) window.alert(Empty From field! Please correct) >
Additional JavaScript code for the smart Send eMail button that would not allow itself to be 34 clicked if the From text field is left blank
<INPUT Event type=submit Handler name=sendEmail value=Send eMail onMouseOver= if (document.sendEmail.sender.value.length < 1) window.alert(Empty From field! Please correct) >
35
This is one way of including JavaScript code in an HTML document that is, including a short JavaScript segment as part of an HTML tag There are a few others as well. Lets now find out about another. But before we do that lets just make clear why we are interested in including JavaScript in our Web pages
36
Why JavaScript?
HTML is great for static Web pages; however, supports only rudimentary interactivity through forms and hyperlinks
JavaScript can be used (along with HTML) to develop interactive content for the Web
37
What is JavaScript?
A programming language specifically designed to work with Web browsers It is designed to be used for developing small programs called scripts that can be embedded in HTML Web pages JavaScript:
Is an interpreted language Supports event-driven programming Is object-based
38
Object Based?
Everything that JavaScript manipulates, it treats as an object e.g. a window or a button An object has properties e.g. a window has size, position, status, etc. Properties are modified with methods e.g. a resize a window with resizeTo(150, 200)
39
40
41
<INPUT type=submit name=sendEmail value=Send eMail onMouseOver= if (document.sendEmail.sender.value.length < 1) window.alert(Empty From field! Please correct) >
42
43
<INPUT type=submit name=sendEmail value=Send eMail onMouseOver= if (document.sendEmail.sender.value.length < 1) window.alert(Empty From field! Please correct) > <INPUT type=submit name=sendEmail value=Send eMail onMouseOver=checkForm() 44 >
checkForm()
JavaScript understands onMouseOver it is one of the pre-defined keywords in JavaScript However, the case for checkForm() is different A checkForm() function does not exist in JavaScript. Therefore, we will have to define it ourselves It can either be defined in the HEAD portion or 45 BODY portion. We will do it in the HEAD.
<HTML> <HEAD> <TITLE>Send an eMail</TITLE> <SCRIPT> function checkForm() { if ( document.sendEmail.sender.value.length < 1) { window.alert( Empty From field! Please correct ); } } </SCRIPT> </HEAD> <BODY bgcolor=#FFFFCC> body content JavaScript code </BODY> enclosed in the new <SCRIPT>,</SCRIPT> </HTML> 46 HTML tags
The put all code in the tag technique seems to require less code
For very short scripts, all code in the tag works well. However, this technique does not work when one needs to put multiple script statements in the same tag
The code in the HEAD portion is more general-purpose, and the right choice for developing larger JavaScript scripts
48
49
The main code segment that goes between the <SCRIPT>, </SCRIPT> tags in the HEAD:
function checkForm() { if ( document.sendEmail.sender.value.length < 1) { window.alert( Empty From field! Please correct ); } }
The JavaScript code included as an attribute of the Send eMail button: onMouseOver=checkForm()
50
Today we checked if the From field of the form was empty How can we modify the JavaScript code for checking if the To field is empty as well? How about checking all four fields? How about checking if the addresses given in the From and To fields are legal eMail addresses? Please try thinking about those possibilities? 51
In Todays Lecture
We learnt ways of constructing forms that were a bit more interactive We got our first taste of JavaScript the object-based language that we will be employing throughout the rest of the Web development part of this course
Last time we mentioned server-side scripts; today we wrote (simple) client-side scripts in JavaScript 52