1 / 70

Introduction to Web Application

Introduction to Web Application. Implement JavaScript in HTML. References. http://10.89.115.100:8086/material/JavaScriptBibleGoldEn.pdf Web Development/HTML and Dynamic HTML/DHTML Reference http://www.w3.org/DOM. Introduction. Dynamic HTML Object Model

rroyall
Download Presentation

Introduction to Web Application

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Web Application Implement JavaScript in HTML

  2. References • http://10.89.115.100:8086/material/JavaScriptBibleGoldEn.pdf • Web Development/HTML and Dynamic HTML/DHTML Reference • http://www.w3.org/DOM

  3. Introduction • Dynamic HTML Object Model • Allows Web authors to control the presentation of their pages • Gives them access to all the elements on their pages • Web page • Elements, forms, frames, tables • Represented in an object hierarchy • Scripting • Retrieve and modify properties and attributes

  4. Object Referencing • The simplest way to reference an element is by using the element’s idattribute. • The element is represented as an object • HTML attributes become properties that can be manipulated by scripting

  5. Example of all <html> <head> <title>Object Model</title> <script type = "text/javascript"> <!-- function start() { alert( pText.innerText ); pText.innerText = "Thanks for coming."; } // --> </script> </head> <body onload = "start()"> <p id = "pText">Welcome to our Web page!</p> </body> </html>

  6. Collections all and children • Collections • Arrays of related objects on a page • all • all the HTML elements in a document • children • Specific element contains that element’s child elements

  7. <html> <head> <title>Object Model</title> <script type = "text/javascript"> <!-- var elements = ""; function start() { for ( var loop = 0; loop < document.all.length; ++loop ) elements += "<br />" + document.all[ loop ].tagName; pText.innerHTML += elements; alert( elements ); } // --> </script> </head> <body onload = "start()"> <p id = "pText">Elements on this Web page:</p> </body> </html>

  8. <html> <head> <title>Object Model</title> <script type = "text/javascript"> <!-- var elements = "<ul>"; function child( object ) { var loop = 0; elements += "<li>" + object.tagName + "<ul>"; for ( loop = 0; loop < object.children.length; loop++ ) { if ( object.children[ loop ].children.length ) child( object.children[ loop ] ); else elements += "<li>" + object.children[ loop ].tagName + "</li>"; } elements += "</ul>" + "</li>"; } // --> </script> </head>

  9. <body onload = "child( document.all[ 4 ] ); myDisplay.outerHTML += elements; myDisplay.outerHTML += '</ul>';"> <p>Welcome to our <strong>Web</strong> page!</p> <p id = "myDisplay"> Elements on this Web page: </p> </body> </html>

  10. The Document Object Model • The DOM is an abstract model that defines the interface between HTML documents and application programs • Documents in DOM have a tree-like structure

  11. The Document Object Model

  12. The Document Object Model • Under development by w3c since the mid-90s • DOM is a successor to DHTML • DOM now has 4 levels • DOM0 is designed to deal with just HTML documents • DOM1 is focused on the HTML and XML document model, all modern browser has supported DOM1 • Appendix

  13. The Document Object Model • DOM 2 is the latest approved standard, which specifies a CSS object model and include document traversals and an event model, but none modern browser can fully support DOM2 • DOM3 is under development

  14. The Document Object Model • It is an OO model - document elements are objects • A language that supports the DOM must have a binding to the DOM constructs • In the JavaScript binding, HTML elements are represented as objects and element attributes are represented as properties • e.g., <input type = "text" name = "address">would be represented as an object with two properties, type and name, with the values "text" and "address"

  15. The Document Object Model • The property names in JavaScript are usually just lowercase versions of the corresponding HTML attribute names

  16. Dynamic Styles • Element’s style can be changed dynamically • Dynamic HTML Object Model also allows you to change the class attribute

  17. <html > <head> <title>Object Model</title> <script type = "text/javascript"> <!-- function start() { var inputColor = prompt( "Enter a color name for the " + "background of this page", "" ); document.body.style.backgroundColor = inputColor; } // --> </script> </head> <body onload = "start()"> <p>Welcome to our Web site!</p> </body> </html>

  18. <html> <head> <title>Object Model</title> <style type = "text/css"> .bigText { font-size: 3em; font-weight: bold } .smallText { font-size: .75em } </style> <script type = "text/javascript"> <!-- function start() { var inputClass = prompt( "Enter a className for the text " + "(bigText or smallText)", "" ); pText.className = inputClass; } // --> </script> </head> <body onload = "start()"> <p id = "pText">Welcome to our Web site!</p> </body> </html>

  19. Dynamic Positioning • HTML elements can be positioned with scripting • Declare an element’s CSS position property to be either absolute or relative • Move the element by manipulating any of the top, left, right or bottom CSS properties

  20. <html> <head> <title>Dynamic Positioning</title> <script type = "text/javascript"> <!-- var speed = 5; var count = 10; var direction = 1; var firstLine = "Text growing"; var fontStyle = [ "serif", "sans-serif", "monospace" ]; var fontStylecount = 0; function start() { window.setInterval( "run()", 100 ); } function run()….. // --> </script> </head> <body onload = "start()"> <p id = "pText" style = "position: absolute; left: 0; font-family: serif; color: blue"> Welcome!</p> </body> </html>

  21. function run() { count += speed; if ( ( count % 200 ) == 0 ) { speed *= -1; direction = !direction; pText.style.color = ( speed < 0 ) ? "red" : "blue" ; firstLine = ( speed < 0 ) ? "Text shrinking" : "Text growing"; pText.style.fontFamily = fontStyle[ ++fontStylecount % 3 ]; } pText.style.fontSize = count / 3; pText.style.left = count; pText.innerHTML = firstLine + "<br /> Font size: " + count + "px"; }

  22. Using the frames Collection • Referencing elements and objects in different frames by using the frames collection

  23. Index.html <html> <head> <title>Frames collection</title> </head> <frameset rows = "100, *"> <frame src = "top.html" name = "upper" /> <frame src = "" name = "lower" /> </frameset> </html>

  24. top.html <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>The frames collection</title> <script type = "text/javascript"> <!-- function start() { var text = prompt( "What is your name?", "" ); parent.frames( "lower" ).document.write( "<h1>Hello, " + text + "</h1>" ); } // --> </script> </head> <body onload = "start()"> <h1>Cross-frame scripting!</h1> </body> </html>

  25. navigator Object • Netscape, Mozilla, Microsoft’s Internet Explorer • Others as well • Contains information about the Web browser • Allows Web authors to determine what browser the user is using

  26. <html> <head> <title>The navigator Object</title> <script type = "text/javascript"> <!-- function start() { if ( navigator.appName == "Microsoft Internet Explorer" ) { if ( navigator.appVersion.substring( 1, 0 ) >= "4" ) document.location = "newIEversion.html"; else document.location = "oldIEversion.html"; } else document.location = "NSversion.html"; } // --> </script> </head> <body onload = "start()"> <p>Redirecting your browser to the appropriate page, please wait...</p> </body> </html>

  27. Summary of the DHTML Object Model window document all frames anchors document history applets document navigator body plugins location embeds event filters screen forms images links Key object plugins collection scripts styleSheets Fig. 13.10 DHTML Object Model.

  28. Summary of the DHTML Object Model

  29. Summary of the DHTML Object Model

  30. Summary of the DHTML Object Model

  31. Event Model

  32. Introduction • Event model • Scripts can respond to user • Content becomes more dynamic • Interfaces become more intuitive

  33. Event onclick • onClick • Invoked when user clicks the mouse on a specific item

  34. <html> <head> <title>DHTML Event Model - onclick</title> <script type = "text/javascript" for = "para" event = "onclick"> <!-- alert( "Hi there" ); // --> </script> </head> <body> <!-- The id attribute gives a unique identifier --> <p id = "para">Click on this text!</p> <!-- You can specify event handlers inline --> <input type = "button" value = "Click Me!" onclick = "alert( 'Hi again' )" /> </body> </html>

  35. Event onload • onload event • Fires when an element finishes loading • Used in the body element • Initiates a script after the page loads into the client

  36. <html> <head> <title>DHTML Event Model - onload</title> <script type = "text/javascript"> <!-- var seconds = 0; function startTimer() { // 1000 milliseconds = 1 second window.setInterval( "updateTime()", 1000 ); } function updateTime() { seconds++; soFar.innerText = seconds; } // --> </script> </head> <body onload = "startTimer()"> <p>Seconds you have spent viewing this page so far: <strong id = "soFar">0</strong></p> </body> </html>

  37. Error Handling with onerror • onerror event • Execute specialized error-handling code

  38. <html> <head> <title>DHTML Event Model - onerror</title> <script type = "text/javascript"> <!-- window.onerror = handleError; function doThis() { alrrt( "hi" ); // alert misspelled, creates an error } function handleError( errType, errURL, errLineNum ) { window.status = "Error: "+errType + " on line " + errLineNum; return true; } // --> </script> </head> <body> <input id = "mybutton" type = "button" value = "Click Me!" onclick = "doThis()" /> </body> </html>

  39. Tracking the Mouse with Event onmousemove • onmousemove • Fires repeatedly when the user moves the mouse over the Web page • Gives position of the mouse

  40. <html> <head> <title>DHTML Event Model - onmousemove event</title> <script type = "text/javascript"> <!-- function updateMouseCoordinates() { coordinates.innerText = event.srcElement.tagName + " (" + event.offsetX + ", " + event.offsetY + ")"; } // --> </script> </head> <body style = "background-color: wheat" onmousemove = "updateMouseCoordinates()"> <span id = "coordinates">(0, 0)</span><br /> <img src = "deitel.gif" style = "position: absolute; top: 100; left: 100" alt = "Deitel" /> </body> </html>

More Related