1 CS428 Web Engineering Lecture 11 Client Side Scripting (JavaScript - I)

Slides:



Advertisements
Similar presentations
Chapter 7 JavaScript: Introduction to Scripting. Outline Simple Programs Objects and Variables Obtaining User Input with prompt Dialogs – –Dynamic Welcome.
Advertisements

WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
Essentials for Design JavaScript Level One Michael Brooks
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
The Web Warrior Guide to Web Design Technologies
NAMEd anchors Enabling users to jump to specific points within Web documents.
1 Chapter 12 Working With Access 2000 on the Internet.
HTML Comprehensive Concepts and Techniques Second Edition Project 8 Integrating JavaScript with HTML.
Javascript Document Object Model. How to use JavaScript  JavaScript can be embedded into your html pages in a couple of ways  in elements in both and.
Information Technology Center Hany Abdelwahab Computer Specialist.
CM143 - Web Week 2 Basic HTML. Links and Image Tags.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
NAMEd anchors Enabling users to jump to specific points within Web documents.
Web Development & Design Foundations with XHTML Chapter 14 Key Concepts.
Inline, Internal, and External FIle
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
Web Programming Material From Greenlaw/Hepp, In-line/On-line: Fundamentals of the Internet and the World Wide Web 1 Introduction The JavaScript Programming.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
1 CS101 Introduction to Computing Lecture 32 Event Handling (Web Development Lecture 11)
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
JavaScript and The Document Object Model MMIS 656 Web Design Technologies Acknowledgements: 1.Notes from David Shrader, NSU GSCIS 2.Some material adapted.
Introduction to JavaScript. JavaScript Facts A scripting language - not compiled but interpreted line by line at run-time. Platform independent. It is.
The Characteristics of CSS
XHTML Introductory1 Forms Chapter 7. XHTML Introductory2 Objectives In this chapter, you will: Study elements Learn about input fields Use the element.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
1 CS101 Introduction to Computing Lecture 18 Objects, Properties, Methods (Web Development Lecture 6)
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript Part 1.
1 VU. 2 CS101 Introduction to Computing Lecture 15 More on Interactive Forms (Web Development Lecture 5)
Introduction to JavaScript + More on Interactive Forms.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
JSP Java Server Pages Softsmith Infotech.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and check boxes using HTML Add a pull-down.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
Active Server Pages  In this chapter, you will learn:  How browsers and servers interacted on the Internet when the Internet first became popular 
Chapter 8 Collecting Data with Forms. Chapter 8 Lessons Introduction 1.Plan and create a form 2.Edit and format a form 3.Work with form objects 4.Test.
JavaScript. During the last lecture We looked at the utility of forms on Web pages We found out about the various components that are used in a form We.
HTML: Hyptertext Markup Language Doman’s Sections.
Introduction to JavaScript CS101 Introduction to Computing.
44238: Dynamic Web-site Development Client Side Programming Ian Perry Room:C48 Extension:7287
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Introduction to JavaScript Objects, Properties, Methods.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
ASP-2-1 SERVER AND CLIENT SIDE SCRITPING Colorado Technical University IT420 Tim Peterson.
Spiderman ©Marvel Comics Creating Web Pages (part 1)
Web Programming Overview. Introduction HTML is limited - it cannot manipulate data How Web pages are extended (include): –Java: an object-oriented programming.
CHAPTER TWO HTML TAGS. 1.Basic HTML Tags 1.1 HTML: Hypertext Markup Language  HTML stands for Hypertext Markup Language.  It is the markup language.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
COM621: Advanced Interactive Web Development Lecture 6 – JavaScript (cont.)
Introduction to.
DHTML.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Web Development & Design Foundations with HTML5 7th Edition
During the last lecture we continued our discussion on Interactive Forms
Tutorial 6 Creating Dynamic Pages
Web Programming and Design
Presentation transcript:

1 CS428 Web Engineering Lecture 11 Client Side Scripting (JavaScript - I)

2 In Today’s Lecture … We will get our first taste of JavaScript – the object-based language. Today we will write (simple) client-side scripts in JavaScript. We will become able to appreciate the concept of objects in JavaScript. We will learn about the properties & methods of those objects, and how to read & modify them.

3 HTML CSS JavaScript markup language content style sheet language presentation programming language behavior

HTML: is a language for content and structure of the page. How many divisions in your page, how many paragraphs, what will be the content of those paragraphs. CSS: style sheet language for presentation, what fonts does the headings have, what will the background color of the page, what’s the width of div. JavaScript: the programming language, for behavior and interactivity, what happens when you mouse over a menu, what happens when you typed the wrong value in a form field. How long does the photo slide show takes to move from one image to the next. 4

5 Now let’s take a look at a form that we constructed, and see how we can make it better

6

7 Let’s now review what happens when I enter all the required info and press the “Send ” button?

8 Info contained in the form Acknowledgement User’s Computer Web Server Server-Side Script Browser Message to the receiver’s address

9 This is what happens when the form is filled correctly. What if the form is filled incorrectly? What if the users leaves one of the essential fields, blank? What if the user enters an illegal address? Examples: –altaf2cs.uol.edu.pk

10 A Reasonable Scenario When the “Send ” 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 user’s browser stating the problem and asks the user to re-send

11 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 server’s 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

12 Client-Side Scripting is a viable alternate In this technique, one uses the user’s browser to checking the form data If data is missing or is incorrect, the browser can prompt the user to take corrective action This way, the form data is sent to the server- side script only after it has been established that the collected data is complete and correct

13 Server-Side Scripts: Review Are programs that reside on Web servers Receive info that a user enters in a form Process that info and take appropriate action Examples: –CGI scripts on Unix servers –ASP/PHP scripts on Windows servers

14 New Concept: Client-Side Scripts Small programs that are a part of the Web page and run on the user’s (client’s) computer They interact with the user to collect info or to accomplish other tasks Once it has been collected, they may help pass the collected info on to a server-side script We’ll use JavaScript to do client-side scripting. However, there are many other languages that can be used for that purpose, e.g. VBScript, JScript

JavaScript a CLIENT-SIDE Language 15 User’s Computer Web Server Browser HTML CSS HTML CSS JavaScript

16 Advantages of Client-Side Scripting Reduced server load as it does not have to send messages to the user’s browser about missing or incorrect data Reduced network traffic as the form’s data is sent only once instead of many to’s and fro’s

17 Disadvantages Client-side scripts do not work with all browsers Some user intentionally turn scripting off or disabled 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

18 Some of things that JavaScript cannot do! The following file ops. on the client computer: –Read-- Modify –Rename-- Delete –Create Create graphics (although, it does have the ability to format pages through HTML - including the placement of graphics) Any network programming bar one function: the ability to download a file to the browser specified through an arbitrary URL

There is no works in JavaScript to talk with a database. You can’t access hardware (USB, etc) 19

20 Some of the things that JavaScript can do! 1.Control the appearance of the browser. 2.Control the content and appearance of the document displayed in the browser 3.Interact with the user through event handlers 4.Arbitrary calculations, including floating-point ones 5.Store & modify a limited amount of data about the user in the form of client-side “cookies”

COOKIES Cookies are small files, in which browser can store some data on client machine, so that next time user visit the same webpage. The webpage get to know where he is actually. Example: In cookies, there is information about client machine address, name, time and date. When i visit amazon.com for book purchase, it is written on the webpage, hello Yasir. 21

What is a Scripting Language? JavaScript is intentionally limited, you can’t write desktop application in JavaScript the way you do in C++, Java or.Net JavaScript only works inside another application the web browser, all browsers have JavaScript engine inside them. The OS runs the web browser, the web browser contains the page and the page contains the JavaScript. 22 operating system C++ AppJava App.Net App Web Browser Web page JavaScript

23 A Simple Example of Client-Side Scripting

24

25 Code for the simple “Send ” button as was described during the last Web development course

26 Additional JavaScript code for the smart “Send ” button that would not allow itself to be clicked if the “From” text field is left blank

27 Event Handler

28 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. Let’s now find out about another. But before we do that … … let’s just make clear why we are interested in including JavaScript in our Web pages

29 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

30 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 language

31 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)

32 Not Object-Oriented! JavaScript is not a true object-oriented language like C++ or Java It is so because it lacks two key features: –A formal inheritance mechanism –Strong typing Nevertheless, it does include many key concepts that are part of almost all object- oriented languages, and therefore is referred as an object-based language

33 Back to our example …

34

35

36

37

38 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 BODY portion. We will do it in the HEAD.

39 Send an function checkForm() { if ( document.send .sender.value.length < 1) { window.alert( “Empty From field! Please correct” ); } } … body content … JavaScript code enclosed in the new, HTML tags

40 We have looked at 2 techniques for embedding JavaScript code in a Web page: 1. Put the code in the tag for the “Send ” button - as was shown to you earlier 2. Put the checkForm() code in the HEAD portion & put the onMouseOver=“checkForm()” attribute in the tag for the “Send ” button Both perform the required function satisfactorily. Q: Which of two techniques is better?

41 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

42 Let’s again look at the JavaScript code

43 The main code segment that goes between the, tags in the HEAD: function checkForm() { if ( document.send .sender.value.length < 1) { window.alert( “Empty From field! Please correct” ); } The JavaScript code included as an attribute of the “Send ” button: onMouseOver=“checkForm()”

44 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 addresses? Please try thinking about those possibilities?

45 Client-Side JavaScript Although a version of JavaScript exists that can be used to write server-side scripts, our focus in this course will only be on client-side scripting

46 Case Sensitivity HTML is not case sensitive. The following mean the same to the browser: – -- JavaScript is case sensitive. Only the first of the following will result in the desired function – the rest will generate an error or some other undesirable event: –onMouseClick-- OnMouseClick –onmouseclick-- ONMOUSECLICK

47 alert(“hello world”); Alert(“hello world”);

48 Object: A named collection of properties (data, state) & methods (instructions, behavior) prop 1 prop 2 prop 5 name prop 3 prop 4 A collection of properties & methods All objects have the “name” property: it holds the name of the object (collection) method 3 method 1 method 2

49 Example: A Bicycle color height direction name pressure speed turn() inflate() accelerate() park()

50 Example: JavaScript’s “window” Object width height status name document location alert() close() open()

51 Properties Objects may have a single or several properties A property may have one of the following values: –Number-- Text-- Boolean –Array-- Functions –Objects (Example: “document” – a property of the “window” object – is an object in itself. A “document” in turn may contain a “form” object as a property, and then that “form” may contain a “button” property, which, once again, is an object in itself)

52 Referring to a Property objectName.propertyName Examples: window.width button.value dot

53

54

55 Change Property Demo # 1 function changeStatus() { window.status = “Mouse has touched the button”; } Change Property Demo # 1

56 The main code segment that goes between the, tags in the HEAD : function changeStatus() { window.status=“Mouse has touched the button”; } The JavaScript code included as an attribute of the “Submit” button: onMouseOver=“changeStatus()” property new value

57

58

59 The main code segment that goes between the, tags in the HEAD : function gotoURL() { window.location=“ } The JavaScript code included as an attribute of the “Go to UOL” button: onMouseOver=“gotoURL()” property new value

60 You should be connected to the Internet for the successful execution of the example that we just discussed

61 A Suggestion Please try the pieces of code that I just demonstrated to you to change the status and location properties of the “window” object yourself Also try changing the width, height properties of the “window” object

62 Types of Objects JavaScript objects –Objects that are part of JavaScript –Examples: window, document Browser objects –Objects that contain info not about the contents of the display, but the browser itself –Examples: history, navigator User-defined object

JavaScript Object Hierarchy 63

Predefined JavaScript Objects 64 AnchorAppletAreaArray BooleanButtonCheckboxDate DocumentEventFileuploadForm FrameFunctionHiddenHistory ImageLayerLinkLocation MathMimetypeNavigatorNumber ObjectOptionPasswordPlugin RadioRegExpResetScreen SelectStringSubmitText Textareawindow

65 Now we have established what we mean by objects: a named collection of properties and methods And that JavaScript treats everything that it manipulates as an object We have also learnt how to change the properties of these objects by selecting a property and equating it to a new value

66 Methods: Functions (code, instructions, behavior) associated with objects Methods are functions associated with an object that can be used to manipulate that object Example: –window.close() –Here “close()” is a method that has been defined for the “window” object. Its function is to close the “window”

67 Referring to a Method objectName.methodName( argumnets ) Examples: window.close() button.click() dot Info is passed on to the method through one or more arguments

68 A few more methods associated with the “window” object alert() confirm() prompt() close() open() focus() blur() setTimeOut() setInterval()

69

70 The main code segment that goes between the, tags in the HEAD : function uolWindow() { window.open(“ } The JavaScript code included as an attribute of the “New UOL Window” button: onClick=“uolWindow()” method argument different event handler

DOM (Document Object Model) What is document? Document simply means a web page. But this document can be represented by different ways. What are the objects? Dates, arrays, images, headers etc What is the model? Is set of terms or standards we are agreed upon. DOM is set of terms that describe exactly how to interact the pieces of a web page. 71

How to get an element node 72 html headbody title h1p ul li About JavaScript Learning JavaScript JavaScript is: a language that runs in browser simple, but powerful misunderstood

You can add id and class attribute to any of your html element. Class can be added to multiple elements. Id should only applies to one element. If element has a unique id you can grab it in JavaScript possibly the most important method of document object which is: 73 document.getElementById(“someId”);

Retrieving an element by ID var myElement = document.getElementById(“abc”); 74 html headbody title h1p ul id =“abc” li

What if i don’t have id or i want to grab more than one element. Notice, its elements the plural. What this is going to do is go through our document and give us back any of the elements that are anchor elements. 75 document.getElementsByTagName(“a”);

Retrieving an element by ID var ListItems = document.getElementsByTagname(“li”); var myLinks = document.getElementsByTagname(“a”); 76 html headbody title h1p ul id=“abc” li ListItems myLinks

77

78

79 The main code segment that goes between the, tags in the HEAD : function changeBgcolor() { window.document.bgColor = “pink”; } The JavaScript code included as an attribute of the “Change Color” button: onMouseOver=“changeBgcolor()” property new value

80 In addition to “bgColor”, there are many other properties of the “document” object, e.g. fgColor linkColor title URL referrer lastModified cookie forms[ ] images[ ] links[ ] …

81 Popup Boxes Alert Box window.alert(“Hello Every body!!"); Confirm Box window.confirm(" Press a button "); Prompt Box window.prompt(“Your name","")

82

83 Read-Only Properties

84 We have learnt how to modify the properties of objects But the properties are not there just so that we can modify them; we can also just read them – that is just find out their current value Let us now look at an example where we first read a property, display the current value, and then change the property

85

86

87 The main code segment that goes between the, tags in the HEAD : function changeBgcolor() { window.document.bgColor = “pink”; oldColor = window.document.bgColor; window.alert("The old color was " + oldColor); } The JavaScript code included as an attribute of the “Change Color” button: onMouseOver=“changeBgcolor()”

88 Event Handlers Objects are made up of properties and associated methods Many objects also have “event handlers” associated with them “Events” are actions that occur as a result of user’s interaction with the browser We use “event handlers ” [e.g. onMouseOver(), onClick()] to design Web pages that can react to those events More on event handlers in a future lecture