javascript
play

JAVASCRIPT an d th e We b ! JAVASCRIPT popular scripting language - PowerPoint PPT Presentation

CS498RK SPRING 2020 JAVASCRIPT an d th e We b ! JAVASCRIPT popular scripting language on the Web, supported by browsers separate scripting from structure (HTML) and presentation (CSS) client- and server-side programming object-oriented,


  1. CS498RK SPRING 2020 JAVASCRIPT an d th e We b !

  2. JAVASCRIPT popular scripting language on the Web, supported by browsers separate scripting from structure (HTML) and presentation (CSS) client- and server-side programming object-oriented, imperative, functional

  3. HOW TO EMBED JS IN HTML Embed external file <script type=“text/javascript" src=“code.js”></script> Inline in HTML <script type="text/javascript"> Javascript goes here... </script>

  4. Revisitin g th e Do m

  5. THE document OBJECT root node of HTML document selector properties/methods: document.body document.getElementById() document.getElementsByClassName() document.getElementsByTagName()

  6. THE HTMLElement OBJECT Node Element HTMLElement From Element From Node element .attributes element .nodeName element .className element .nodeType element .id element .textContent element .innerHTML element .tagName

  7. THE HTMLElement OBJECT properties for traversing the DOM tree element.childNodes element.children element.parentNode element.parentElement VS. element.previousSibling element.previousElementSibling element.nextSibling element.nextElementSibling A Node can be anything in the DOM (Text, Comments, etc.), while Elements are the nodes that represent HTML elements

  8. TRAVERSING THE DOM BODY var body = document.body; var div = body.children[0]; DIV var h3 = div.children[0]; var textNode = h3.childNodes[0]; H3 IMG var textString = textNode.nodeValue; “My first photo”

  9. THE HTMLElement OBJECT relative to offsetParent MARGIN BORDER position: element.offsetTop , PADDING element.scrollTop , … CONTENT dimensions: element.clientWidth , clientWidth element.offsetWidth , … style: element.style offsetWidth (includes scrollbar)

  10. DOM MANIPULATION programmatically change the structure and modify element properties element.style.backgroundColor = "red"; element.innerHTML = "<div><h3>Llama!</h3>…</div>" augment DOM structure: element.appendChild() , element.removeChild() , …

  11. Event s

  12. TYPES OF EVENTS User mouse clicks/moves, key presses Browser page load/unload Network responses to AJAX request Timer

  13. TIMER EVENTS setTimeout( fn , ms ); calls function after specified amount of time (ms) setInterval( fn , ms ); calls function at specified intervals (ms) until clearInterval() or window is closed

  14. EVENT HANDLERS als o know n a s listene rs make use of callback functions specify what happened, where it happened, and how to handle it

  15. EVENT HANDLERS In HTML DOM LEVEL 0 <div onclick="alert('Llama!');">…</div> In Javascript using the DOM DOM LEVEL 1 element.onclick = function(){alert('Llama!');}

  16. EVENT HANDLERS DOM LEVEL 2 var el = document.getElementById(‘myButton'); el.addEventListener('click', function(){ alert('Llama!'); }); supports multiple handlers per event!

  17. THE BROWSER EVENT LOOP Parse HTML and create DOM USER BROWSER Check for events Event Queue NETWORK TIMER NO Event? YES single-threaded: events processed one at a time Process event

  18. EVENT OBJECT contains the information about the event HTML <div onclick="mouseClick(event);"> DOM element.onclick = mouseClick; function mouseClick(event){…};

  19. EVENT PROCESSING events propagate in two phases capture phase: root to innermost element bubble phase: innermost element to root DOM standard: capture then bubble

  20. EVENT PROCESSING element.addEventListener( event , function , useCapture ) se t captur e o r bubbl e phas e event.stopPropogation() CodePen

  21. Even t Exampl e 1 CodePen

  22. Anonymou s Function s function animateIt(elementId, speed) { var elem = document.getElementById(elementId); var tick = 0; var timer = setInterval(function() { if (tick < 100) { elem.style.left = tick * speed + "px"; tick++; } else { clearInterval(timer); } }, 30); }

  23. Cl o sure s function animateIt(elementId, speed) { var elem = document.getElementById(elementId); var tick = 0; var timer = setInterval(function() { if (tick < 100) { elem.style.left = tick * speed + "px"; tick++; } else { clearInterval(timer); } }, 30); }

  24. Even t Exampl e 2 CodePen

  25. Classe s an d Mous e Event s function Dragger(id) { this.isMouseDown = false; this.element = document.getElementById(id); var obj = this; this.element.onmousedown = function(event) { obj.mouseDown(event); } } why obj instead of this?

  26. Classe s an d Mous e Event s Dragger.prototype.mouseDown = function(event) { var obj = this; this.oldMoveHandler = document.body.onmousemove; document.body.onmousemove = function(event) { obj.mouseMove(event);} this.oldUpHandler = document.body.onmouseup; document.body.onmouseup = function(event) { obj.mouseUp(event);} this.oldX = event.clientX; this.oldY = event.clientY; this.isMouseDown = true; why body? }

  27. Trouble s wit h Bro w se rs an d Othe r Quirk s

  28. BROWSERS stable APIs, but different implementations JavaScript libraries duplicate existing event handling and DOM APIs

  29. JQUERY cross-browser compatibility use for all DOM manipulation: (e.g., positioning relative to document and not offsetParent) jquery.com CodePen

  30. dorey.github.io/JavaScript-Equality-Table/

  31. NEXT CLASS: ADVANCED JS/CSS https://uiuc-web-programming.gitlab.io/sp20/

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend