Presentation is loading. Please wait.

Presentation is loading. Please wait.

Page 1 of 39 Javascript Chapters 13, 14 Vadim Parizher Computer Science Department California State University, Northridge Fall 2003 Slides from text Book.

Similar presentations


Presentation on theme: "Page 1 of 39 Javascript Chapters 13, 14 Vadim Parizher Computer Science Department California State University, Northridge Fall 2003 Slides from text Book."— Presentation transcript:

1 Page 1 of 39 Javascript Chapters 13, 14 Vadim Parizher Computer Science Department California State University, Northridge Fall 2003 Slides from text Book by Deitel, Deitel & Nieto These slides are only for use in connection with the CPMP 496EBT course Copying other than for private study strictly prohibitted © Same as the book

2 Page 2 of 39 Dynamic HTML object model  Provides control over presentation of pages  Access to all elements on the page  Whole web page (elements, forms, frames, tables, etc.) represented in an object hierarchy  HTML elements treated as objects  Attributes of these elements treated as properties of those objects  Objects identified with an ID attribute can be scripted with languages like JavaScript, JScript and VBScript  Simplest way to reference an element is by its ID attribute  Objects have properties  innertext, htmltext, tagname, etc  These properties can be changed by scripting to create dynamic pages.

3 Page 3 of 39 innertext property  innertext property  Object pText refers to the P element whose ID is set to pText (line 22)  innertext property refers to text contained in element 1 2 3 4 5 6 7 8 Object Model 9 10 11 function start() 12 { 13 alert( pText.innerText ); 14 pText.innerText = "Thanks for coming."; 15 } 16 17 18 19 20 21 22 Welcome to our Web page! 23 24 25

4 Page 4 of 39 Output

5 Page 5 of 39 DHTML object model window document history navigator applets all anchors body embeds forms filters images links plugins styleSheets scripts location screen event document plugins object collection Key frames

6 Page 6 of 39 IE 5 Objects

7 Page 7 of 39 IE 5 Objects

8 Page 8 of 39 IE 5 Collections of Objects  Collections are arrays of related objects on a page

9 Page 9 of 39 IE 5 Collections of Objects

10 Page 10 of 39 The Navigator Object  navigator object  Supported by Netscape Navigator and Internet Explorer  navigator object contains info about the Web browser viewing the page  navigator.appName contains the name of the application  “Microsoft Internet Explorer”  “Netscape”  Value of navigator.appVersion not a simple integer  Contains other info, such as OS  When using a browser- specific technology  Make provisions for other browsers 1 2 3 4 5 6 7 8 The navigator Object 9 10 11 function start() 12 { 13 if ( navigator.appName == "Microsoft Internet Explorer" ) { 14 15 if ( navigator.appVersion.substring( 1, 0 ) >= "4" ) 16 document.location = "newIEversion.html"; 17 else 18 document.location = "oldIEversion.html"; 19 } 20 else 21 document.location = "NSversion.html"; 22 } 23 24 25 26 27 28 Redirecting your browser to the appropriate page, 29please wait... 30 31 32 URL of active document

11 Page 11 of 39 The Event Model  Scripts respond to user actions and change page accordingly  Moving mouse, Scrolling screen, entering keystrokes, etc  ONCLICK event, fires when user clicks mouse  ONLOAD event, fires when element finishes loading successfully and can start a script

12 Page 12 of 39 ONCLICK 1 2 3 4 5 6 7 8 DHTML Event Model - ONCLICK 9 10 11 12 13 14 alert( "Hi there" ); 15 16 17 18 19 20 21 22 Click on this text! 23 24 25<INPUT TYPE = "button" VALUE = "Click Me!" 26 ONCLICK = "alert( 'Hi again' )"> 27 28 29  Can be invoked with a function or coded inline

13 Page 13 of 39 ONLICK Output Executes because of script lines 11-15 Executes because of event handler on line 25

14 Page 14 of 39 ONLOAD  ONLOAD event in BODY triggers startTimer 1 2 3 4 5 6 7 DHTML Event Model - ONLOAD 8 9 10var seconds = 0; 11 12function startTimer(){ 13 // 1000 milliseconds = 1 second 14 window.setInterval( "updateTime()", 1000 ); 15} 16 17function updateTime(){ 18 seconds++; 19 soFar.innerText = seconds; 20} 21 22 23 24 25 26 27 Seconds you have spent viewing this page so far: 28 0 29 30 31

15 Page 15 of 39 ONLOAD Output

16 Page 16 of 39 ONERROR  Use ONERROR event can be used to print customer friendly error messages (Browser default msgs are ugly!)  Has three parameters  Type of error  URL of file with error  Line number of error

17 Page 17 of 39 ONERROR  Function handleError executes when ONERROR event triggered in window object  Return true to event handler to cancel browser’s default response 1 2 3 4 5 6 7 8 DHTML Event Model - ONERROR 9 10 11// Specify that if an ONERROR event is triggered in the window 12// function handleError should execute 13window.onerror = handleError; 14 15function doThis() { 16 alrrt( "hi" ); // alert misspelled, creates an error 17} 18 19// The ONERROR event passes three values to the function: the 20// name of the error, the url of the file, and the line number. 21function handleError( errType, errURL, errLineNum ) 22{ 23 // Writes to the status bar at the bottom of the window. 24 window.status = "Error: " + errType + " on line " + 25 errLineNum; 26 27 // Returning a value of true cancels the browser’s reaction. 28 return true; 29} 30 31

18 Page 18 of 39 ONERROR Output 32 33 34 35 36<INPUT ID = "mybutton" TYPE = "button" VALUE = "Click Me!" 37 ONCLICK = "doThis()"> 38 39 40 Custom error output

19 Page 19 of 39 ONMOUSEMOVE event  ONMOUSEMOVE event fires constantly whenever mouse moves  event object contains info about triggered event  srcElement - Pointer to element object that triggered event  offsetX and offsetY location of mouse cursor relative to top- left corner of object in which event triggered

20 Page 20 of 39  Function updateMo useCoord inates uses properties of event object to track mouse cursor 1 2 3 4 5 6 7 8 DHTML Event Model - ONMOUSEMOVE event 9 10 function updateMouseCoordinates() 11 { 12 coordinates.innerText = event.srcElement.tagName + 13 " (" + event.offsetX + ", " + event.offsetY + ")"; 14 } 15 16 17 18 19 20 21 (0, 0) 22<IMG SRC = "deitel.gif" STYLE = "position: absolute; top: 100; 23 left: 100"> 24 25 26 ONMOUSEMOVE Gives the id coordinates to the block of text between SPAN … /SPAN Current event Element where event occurred Tag of element referenced Locn of mouse cursor (relative to top L corner of object

21 Page 21 of 39 Output Updated text (keeps changing as you move the mouse)

22 Page 22 of 39 Properties of the event object

23 Page 23 of 39 Other Mouse Events  ONMOUSEOVER event  Fires when mouse cursor moves over an element  ONMOUSEOUT event  Fires when mouse cursor leaves the element  Combine events for rollover effect  Pre-load images  Read the code and try this program out!!

24 Page 24 of 39 ONFOCUS and ONBLUR  Used for form processing  ONFOCUS - fires when an element gains focus  User clicks on form field  User uses Tab key to highlight element  ONBLUR - fires when an element loses focus  ONSUBMIT - fires when a form is submitted  ONRESET - fires when a form is reset

25 Page 25 of 39 ONFOCUS ONBLUR output  Help msg based on mouse position

26 Page 26 of 39 Form processing with onfocus.. 1 2 3 4 5 6 7 8 DHTML Event Model - ONFOCUS and ONBLUR 9 10 11 var helpArray = 12 [ "Enter your name in this input box.", 13 "Enter your email address in this input box, " + 14 "in the format user@domain.", 15 "Check this box if you liked our site.", 16 "In this box, enter any comments you would " + 17 "like us to read.", 18 "This button submits the form to the " + 19 "server-side script", 20 "This button clears the form", 21 "This TEXTAREA provides context-sensitive " + 22 "help. Click on any input field or use the TAB " + 23 "key to get more information about the input field." ]; 24 25 function helpText( messageNum ) 26 { 27 myForm.helpBox.value = helpArray[ messageNum ]; 28 } 29 30 31 32

27 Page 27 of 39 Onfocus/onblur 33 34 35Name: <INPUT TYPE = "text" NAME = "name" 36 ONFOCUS = "helpText(0)" ONBLUR = "helpText(6)"> 37Email: <INPUT TYPE = "text" NAME = "email" 38 ONFOCUS = "helpText(1)" ONBLUR = "helpText(6)"> 39Click here if you like this site 40<INPUT TYPE = "checkbox" NAME = "like" ONFOCUS = "helpText(2)" 41 ONBLUR = "helpText(6)"> 42 43Any comments? 44<TEXTAREA NAME = "comments" ROWS = 5 COLS = 45 ONFOCUS = 45 "helpText(3)" ONBLUR = "helpText(6)"> 46<INPUT TYPE = "submit" VALUE = "Submit" ONFOCUS = "helpText(4)" 47 ONBLUR = "helpText(6)"> 48<INPUT TYPE = "reset" VALUE = "Reset" ONFOCUS = "helpText(5)" 49 ONBLUR = "helpText(6)"> 50 51<TEXTAREA NAME = "helpBox" STYLE = "position: absolute; 52 right: 0; top: 0" ROWS = 4 COLS = 45> 53This TEXTAREA provides context-sensitive help. Click on any 54input field or use the TAB key to get more information about the 55input field. 56 57 58 59

28 Page 28 of 39 ONSUBMIT/ONRESET  ONSUBMIT event  Fires when a form is submitted  ONRESET event  Fires when a form is reset

29 Page 29 of 39 Onsubmit Onreset 1 2 3 4 5 6 7 8 DHTML Event Model - ONSUBMIT and ONRESET events 9 10 11 var helpArray = 12 [ "Enter your name in this input box.", 13 "Enter your email address in this input box, " + 14 "in the format user@domain.", 15 "Check this box if you liked our site.", 16 "In this box, enter any comments you would " + 17 "like us to read.", 18 "This button submits the form to the " + 19 "server-side script", 20 "This button clears the form", 21 "This TEXTAREA provides context-sensitive " + 22 "help. Click on any input field or use the TAB " + 23 "key to get more information about the input field." ]; 24 25 function helpText( messageNum ) 26 { 27 myForm.helpBox.value = helpArray[ messageNum ]; 28 } 29 30 function formSubmit() { 31 window.event.returnValue = false; 32

30 Page 30 of 39 Onsubmit Onreset 33 if ( confirm ( "Are you sure you want to submit?" ) ) 34 window.event.returnValue = true; 35 } 36 37 function formReset() { 38 window.event.returnValue = false; 39 40 if ( confirm ( "Are you sure you want to reset?" ) ) 41 window.event.returnValue = true; 42 } 43 44 45 46 47 48 49<FORM ID = "myForm" ONSUBMIT = "formSubmit()" 50 ONRESET = "formReset()"> 51Name: <INPUT TYPE = "text" NAME = "name" ONFOCUS = "helpText(0)" 52 ONBLUR = "helpText(6)"> 53Email: <INPUT TYPE = "text" NAME = "email" 54 ONFOCUS = "helpText(1)" ONBLUR = "helpText(6)"> 55Click here if you like this site 56<INPUT TYPE = "checkbox" NAME = "like" ONFOCUS = "helpText(2)" 57 ONBLUR = "helpText(6)"> 58 59Any comments? 60<TEXTAREA NAME = "comments" ROWS = 5 COLS = 45 61 ONFOCUS = "helpText(3)" ONBLUR = "helpText(6)"> 62<INPUT TYPE = "submit" VALUE = "Submit" ONFOCUS = "helpText(4)" 63 ONBLUR = "helpText(6)"> 64<INPUT TYPE = "reset" VALUE = "Reset" ONFOCUS = "helpText(5)"

31 Page 31 of 39 Onsubmit Onreset 65 ONBLUR = "helpText(6)"> 66 67<TEXTAREA NAME = "helpBox" STYLE = "position: absolute; right:0; 68 top: 0" ROWS = 4 COLS = 45> 69This TEXTAREA provides context-sensitive help. Click on any 70input field or use the TAB key to get more information about the 71input field. 72 73 74 75

32 Page 32 of 39 Event Bubbling  Event bubbling  Events fired in child elements also “bubble” up to their parent elements for handling  Cancel bubbling using cancelBubb le property of event object  Forgetting to cancel event bubbling when necessary may cause unexpected results in your scripts 1 2 3 4 5 6 7 8 DHTML Event Model - Event Bubbling 9 10 11 function documentClick() 12 { 13 alert( "You clicked in the document" ); 14 } 15 16 function paragraphClick( value ) 17 { 18 alert( "You clicked the text" ); 19 if ( value ) 20 event.cancelBubble = true; 21 } 22 23 document.onclick = documentClick; 24 25 26 27 28 29 Click here! 30 Click here, too! 31 32 This why the bubbling occurs. This has registered a document level even processing. ParagraphClick registers a lower level event processing- text within the document object

33 Page 33 of 39 Output The event has bubbled up to the document level The event has been canceled

34 Page 34 of 39 Clipboard events

35 Page 35 of 39 Data binding events

36 Page 36 of 39 Other Events  Keyboard events  Marquee Events

37 Page 37 of 39 More mouse events

38 Page 38 of 39 Miscellaneous events

39 Page 39 of 39 Miscellaneous events


Download ppt "Page 1 of 39 Javascript Chapters 13, 14 Vadim Parizher Computer Science Department California State University, Northridge Fall 2003 Slides from text Book."

Similar presentations


Ads by Google