SlideShare a Scribd company logo
Communications Lab :: Web Lecture 5: JavaScript
Reminders Assignment #4 due today Final Projects - sign up for a 10-minute talk to plan it out
Today JavaScript Introduction Syntax details: variables, types, conditionals, loops Using the console Accessing HTML Modifying HTML Debugging JavaScript
What is JavaScript JavaScript is text that is fed into a browser that can interpret it and then it is enacted by the browser Because JavaScript code can run locally in a user's browser (rather than on a remote server), the browser can respond to user actions quickly, making an application more responsive Works with document structure created by HTML
 
JavaScript Drag and Drop Form validation  Dynamic drop-down menus (http://sandbox.scriptiny.com/dropdown-menu/) Pop-up windows Alerts
Examples http://www.macs.hw.ac.uk/~ceehsa1/languages/javascript/puzzle1/index.html http://www.macs.hw.ac.uk/~ceehsa1/languages/javascript/puzzle2/index.html http://www.squarebox.co.uk/hcalc.html?0.01745240589744836 http://www.bodo.com/j17.htm   http://www.bodo.com/j19.htm
Java is to JavaScript what Car is to Carpet
Using JavaScript Inline In the <head> tag of your HTML Within the <body> tag of HTML By including an external file (.js)
Using JavaScript Inline   <html>     <head>       ...     </head>     <body>       ...       <a href= &quot;javascript: alert()&quot; ></a>       <a href=&quot;#&quot;  onclick=&quot;alert()&quot; ></a>       ...     </body>  </html>
Using JavaScript 2.  In the <head> tag of your HTML   <html>     <head>       ...       < script  type=&quot; text/javascript &quot;>         alert();       </ script >       ...     </head>     <body>       ...     </body>  </html>
Using JavaScript 3.  Within the <body> tag of HTML   <html>     <head>       ...     </head>     <body>       ...       < script  type=&quot;text/javascript&quot;>         alert ();       </ script>    </body>  </html>
Using JavaScript 4.  By including an external file   <html>     <head>       ...     </head>     <body>       ...       <script type=&quot;text/javascript&quot;              src=&quot;my_javascript_file.js&quot;>      </script>     </body>  </html
JavaScript in Console Open Developer Tools in Chrome (right-click > Inspect Element) Test your JavaScript in the browser!
JavaScript Syntax The  <script> tag  tells the browser to run JavaScript for the text included between the tags Each JavaScript line is  an instruction sent to the browser. Each JavaScript line  ends in a semicolon (;) You can  group several lines of code  together with curly braces ( { .... })
JavaScript Variables var  car = &quot;ford&quot;; var  x = 0; var  y; Variable can have a value associated with it The value of the variable can change during the execution The keyword &quot;var&quot; has to be used in order to  declare the variable . Declaring the variable is done at the top of the script
JavaScript Operations x = 5 + 34; y = 3 - 2; result = 200 / 40; speed = (43 * (23 + 43)) / 4; x++; // x = x + 1; y--; // y = y - 1; x+=y; // x = x + y;
JavaScript Comments // This is a comment /*       This is a ...           two-line comment! */
JavaScript Variable Types Numbers var x = 0;   Strings  - used for representing text  var message = &quot;Please try again.&quot; var car = 'Ford' You can use  either &quot;&quot; or '' Arrays  - a list of values    var arr = [3, 4, 5];   var pets = new Array();
JavaScript Strings Used for variables that hold text   You can  add strings  together var text = &quot;I love &quot; + &quot;mushrooms&quot;; var vegetable = &quot;kale&quot;; text = &quot;I love &quot; + vegetable;       // The value of text will be &quot;I love kale&quot;
JavaScript Arrays var pets= new Array() ;  pets [0] =&quot;cat&quot;;  pets [1] =&quot;dog&quot;; pets [2] =&quot;fish&quot;; var pets= new Array( &quot;cat&quot;,&quot;dog&quot;,&quot;fish&quot; ) ; var pets= [ &quot;cat&quot;,&quot;dog&quot;,&quot;fish&quot; ] ;
JavaScript Arrays var fave_pet = pets[2]  // will get &quot;fish&quot;  pets[2] = parrots; // This reassigns the value var pet_count = pets.length; // Returns 3
JavaScript Conditionals if (condition) {    // code will run if condition is true }
JavaScript Conditionals if (condition){    // code will run if condition is true } else {    // code will run if condition is false }
JavaScript Conditionals if (condition1) {     // code will run if condition1 is true } else if (condition2) {     // code will run if condition2 is true } else {    // code will run if neither condition1 nor condition2 is true }
JavaScript Conditionals x  ==  4    // is value of x equal to number 4? x  !=  4     // is value of x different from number 4? x  >   4     // is value of x greater than number 4? x  <  4      // is value of x smaller than 4? x  >=   4   // is value of x greater or equal to number 4? x  <=  4    // is value of x smaller or equal to 4? if (x  ==  4) {     ......  }
JavaScript For Loops for (var i=0; i<5; i++) {     // lines of code to run for each element    // i = 0 first time loop runs    // i = 1 second time, i = 2 the third time    .....  } Declare a variable named &quot;i&quot;  Assign the value of 0 to variable i Set the upper limit for i. In this case, i cannot exceed 5. i++ increments i by 1 each time the loop runs
JavaScript Alert alert (&quot;Hey there!&quot;);
JavaScript Functions A function contains a block of code that will only run when the function is called Declaring a function: function someFunction (variable1, variable2,..) {     // Code in here will run }
JavaScript Functions To call a function: someFunction (value1, value2, ...);      // declared by us alert (&quot;Hello world!&quot;);      // function native to JavaScript
JavaScript Functions <html> <head> <script type=&quot;text/javascript&quot;> function displayMessage() {    alert(&quot;Confirmed!&quot;); } </script> </head> <body> <a href=&quot;#&quot;  onclick=&quot;displayMessage()&quot; >Click me</a> </body> </html>
JavaScript Events JavaScript detects when a certain action is taking place. Examples of actions detected: The mouse was clicked  The mouse moves The mouse is over an HTML element User pressed a key A page has loaded A page is resizing
JavaScript Events Mouse is over an HTML element <a href=&quot;#&quot;  onmouseover=&quot;alert('You are over link!')&quot; >... User clicks on the HTML element <a href=&quot;#&quot;  onclick=&quot;alert('You are clicking link!')&quot; >...
JavaScript Events The body of the HTML has loaded: <html> <head> <script type=&quot;text/javascript&quot;> function bodyLoaded() {     alert(&quot;Page is loaded&quot;); } </script> </head> < body onload=&quot;bodyLoaded()&quot; >     .... </body> </html> 
JavaScript and HTML <p>Once upon a time...</p> <p>What a great story!</p> document. getElementsByTagName (&quot;p&quot;);
JavaScript and HTML <div id=&quot;title&quot;>Once upon a time...</div> document. getElementById (&quot;title&quot;);
JavaScript and HTML <a href=&quot;#&quot; class=&quot;utility&quot;>Link</a> <a href=&quot;#&quot; class=&quot;utility&quot;>Another Link</a> document. getElementsByClassName (&quot;utility&quot;);
JavaScript and HTML Other properties: parentNode childNodes firstChild lastChild nextSibing previousSibling
JavaScript and HTML <div id=&quot;story&quot;>     <p id=&quot;title&quot;>Once upon a time...</p> </div>   var p = document. getElementById (&quot;title&quot;);   var parent = p.parentNode;    // will be <div id=&quot;story&quot;>
JavaScript and HTML <p>Once upon a time...</p> <p>What a great story!</p> paragraphs = document .getElementsByTagName (&quot;p&quot;);                         //Array paragraphs.length; // Returns 2 paragraphs in array for (i=0; i<paragraphs.length; i++) {      ... }
JavaScript and HTML document .anchors   // Array containing all links document. anchors[2]  // Third link element on page (<a>) document .forms[0]   // Returns the first form HTML element document .forms  // Array of all form elements on page document .images.length  // How many image tags on page document .images  // Array of all image elements on page
JavaScript and HTML Get the contents of an HTML element: <html> <head> <script type=&quot;text/javascript&quot;> function getValue() {    var header_value = document.getElementById(&quot;header&quot;);    alert(header_value .innerHTML ); } </script> </head> <body>    <h1 href=&quot;#&quot; id=&quot;header&quot; onclick=&quot;getValue()&quot;>Click me!</h1> </body> </html>
JavaScript and HTML Return the class name of an HTML element: <div id=&quot;title&quot; class=&quot;large&quot;>Savings</div> document.getElementById(&quot;title&quot;) .className ;      // Will return &quot;large&quot;
JavaScript and HTML Create HTML elements dynamically: var body = document.getElementsByTagName('body')[0]; var newdiv = document .createElement('div') ; newdiv .setAttribute ('id',&quot;images&quot;); newdiv .innerHTML  =      &quot;Images: <img src='plant.jpg' onclick='alert('Plant!')'/>&quot;; body .appendChild(newdiv) ;
JavaScript & HTML & CSS Change the CSS style of an HTML element: <div id=&quot;title&quot; class=&quot;large&quot;>Savings</div> document.getElementById(&quot;title&quot;) .style .fontSize = &quot;14px;&quot; element.style.color element.style.background element.style.backgroundImage element.style.backgroundPosition
JavaScript & HTML & CSS Making an HTML element appear and disappear: <div id=&quot;title&quot; class=&quot;large&quot;>Savings</div> document.getElementById(&quot;title&quot;) .style.display  = &quot;none&quot;; document.getElementById(&quot;title&quot;) .style.display  = &quot;block&quot;;
JavaScript & HTML & CSS CSS selectors are separated by  dash -  : background - image font - size In Javascript, they are  camelCased : background I mage font S ize
Debugging JavaScript var image = &quot;cat.jpg&quot;; console.log (&quot;Value of var 'image' is &quot;, image); console.log (&quot;Value of var 'image' is &quot; +image);   This prints in Developer Inspector tools  Console  tab:    JavaScript  ERRORS  are also found here.
JavaScript Exercises Make a few boxes using CSS and HTML. When you click on them, change their color. Choose a random different color every time, use an array to store them.
Resources Code Academy JavaScript Tutorial: http://www.codecademy.com/ W3Schools JavaScript examples: http://www.w3schools.com/js/js_ex_dom.asp
Next Class... Assignment #5 Due Be ready to present your  final project idea We'll do a recap of some of the concepts we covered Some more advanced Sinatra concepts.

More Related Content

PPT
PPT
Processing XML with Java
PPT
JSP Custom Tags
PPT
Using SVG with Ample SDK cross browser
PPTX
Java script
PPT
Rich faces
PPTX
Introduction to java script
DOC
Introduction to java script
Processing XML with Java
JSP Custom Tags
Using SVG with Ample SDK cross browser
Java script
Rich faces
Introduction to java script
Introduction to java script

What's hot (20)

DOCX
Exception handling
RTF
Java scripts
PPTX
Web programming
ODP
JavaScript and jQuery Fundamentals
DOC
Basics java scripts
ODP
Object Oriented Design Patterns for PHP
PPT
Fundamentals of programming angeli
PPT
Javascript tutorial
PDF
Jscript Fundamentals
PPTX
Javascript
PPT
Dynamic Web Pages Ch 1 V1.0
PPTX
Java script
PPTX
Java script
PPT
Java script
PPT
CSIS 138 Javascript Class1
PPT
Even Faster Web Sites at The Ajax Experience
PPTX
Introduction to JavaScript Programming
PPTX
Javascript conditional statements
PDF
Advanced PHP: Design Patterns - Dennis-Jan Broerse
 
PPT
Changing Template Engine
Exception handling
Java scripts
Web programming
JavaScript and jQuery Fundamentals
Basics java scripts
Object Oriented Design Patterns for PHP
Fundamentals of programming angeli
Javascript tutorial
Jscript Fundamentals
Javascript
Dynamic Web Pages Ch 1 V1.0
Java script
Java script
Java script
CSIS 138 Javascript Class1
Even Faster Web Sites at The Ajax Experience
Introduction to JavaScript Programming
Javascript conditional statements
Advanced PHP: Design Patterns - Dennis-Jan Broerse
 
Changing Template Engine
Ad

Viewers also liked (11)

PPT
WordPress made for humans
PDF
2/2 slave labor on farms
PPT
Lecture 3 Javascript1
PDF
JavaScript introduction 1 ( Variables And Values )
PPT
eXo SEA - JavaScript Introduction Training
PPT
introduction to javascript
PDF
JavaScript Basics And DOM Manipulation
KEY
Introduction to Javascript - College Lecture
PPT
Introduction to JavaScript
PPT
Introduction to Javascript
PPT
JavaScript - An Introduction
WordPress made for humans
2/2 slave labor on farms
Lecture 3 Javascript1
JavaScript introduction 1 ( Variables And Values )
eXo SEA - JavaScript Introduction Training
introduction to javascript
JavaScript Basics And DOM Manipulation
Introduction to Javascript - College Lecture
Introduction to JavaScript
Introduction to Javascript
JavaScript - An Introduction
Ad

Similar to Lecture 5 - Comm Lab: Web @ ITP (20)

PPT
JavaScript
PPT
Javascript survival for CSBN Sophomores
PPT
JavaScript Workshop
PPT
Java script final presentation
PPT
JavaScript & Dom Manipulation
PPTX
JavaScript_III.pptx
PPT
Javascript: Ajax & DOM Manipulation v1.2
PPTX
Javascript Tlabs
PDF
internet Chapter 4-JavascripPrepare a ppt of video compression techniques bas...
PDF
javascriptPresentation.pdf
PPTX
Lecture 5 javascript
PPTX
Java Script Basic to Advanced For Beginner to Advanced Learner.pptx
PPTX
Basics of Java Script (JS)
PPTX
PPT
Java script
PDF
Java Script notes
PPTX
PPT
JavaScript: Ajax & DOM Manipulation
PPTX
Lab #2: Introduction to Javascript
PPTX
First javascript workshop : first basics
JavaScript
Javascript survival for CSBN Sophomores
JavaScript Workshop
Java script final presentation
JavaScript & Dom Manipulation
JavaScript_III.pptx
Javascript: Ajax & DOM Manipulation v1.2
Javascript Tlabs
internet Chapter 4-JavascripPrepare a ppt of video compression techniques bas...
javascriptPresentation.pdf
Lecture 5 javascript
Java Script Basic to Advanced For Beginner to Advanced Learner.pptx
Basics of Java Script (JS)
Java script
Java Script notes
JavaScript: Ajax & DOM Manipulation
Lab #2: Introduction to Javascript
First javascript workshop : first basics

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
Spectroscopy.pptx food analysis technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Review of recent advances in non-invasive hemoglobin estimation
Building Integrated photovoltaic BIPV_UPV.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
MYSQL Presentation for SQL database connectivity
Per capita expenditure prediction using model stacking based on satellite ima...
Machine learning based COVID-19 study performance prediction
Programs and apps: productivity, graphics, security and other tools
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Understanding_Digital_Forensics_Presentation.pptx
Network Security Unit 5.pdf for BCA BBA.
Dropbox Q2 2025 Financial Results & Investor Presentation
20250228 LYD VKU AI Blended-Learning.pptx
cuic standard and advanced reporting.pdf
Spectroscopy.pptx food analysis technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

Lecture 5 - Comm Lab: Web @ ITP

  • 1. Communications Lab :: Web Lecture 5: JavaScript
  • 2. Reminders Assignment #4 due today Final Projects - sign up for a 10-minute talk to plan it out
  • 3. Today JavaScript Introduction Syntax details: variables, types, conditionals, loops Using the console Accessing HTML Modifying HTML Debugging JavaScript
  • 4. What is JavaScript JavaScript is text that is fed into a browser that can interpret it and then it is enacted by the browser Because JavaScript code can run locally in a user's browser (rather than on a remote server), the browser can respond to user actions quickly, making an application more responsive Works with document structure created by HTML
  • 5.  
  • 6. JavaScript Drag and Drop Form validation  Dynamic drop-down menus (http://sandbox.scriptiny.com/dropdown-menu/) Pop-up windows Alerts
  • 7. Examples http://www.macs.hw.ac.uk/~ceehsa1/languages/javascript/puzzle1/index.html http://www.macs.hw.ac.uk/~ceehsa1/languages/javascript/puzzle2/index.html http://www.squarebox.co.uk/hcalc.html?0.01745240589744836 http://www.bodo.com/j17.htm   http://www.bodo.com/j19.htm
  • 8. Java is to JavaScript what Car is to Carpet
  • 9. Using JavaScript Inline In the <head> tag of your HTML Within the <body> tag of HTML By including an external file (.js)
  • 10. Using JavaScript Inline   <html>    <head>      ...    </head>    <body>      ...       <a href= &quot;javascript: alert()&quot; ></a>       <a href=&quot;#&quot; onclick=&quot;alert()&quot; ></a>      ...    </body>  </html>
  • 11. Using JavaScript 2.  In the <head> tag of your HTML   <html>    <head>      ...      < script type=&quot; text/javascript &quot;>        alert();      </ script >      ...    </head>    <body>      ...    </body>  </html>
  • 12. Using JavaScript 3.  Within the <body> tag of HTML   <html>    <head>      ...    </head>    <body>      ...      < script type=&quot;text/javascript&quot;>        alert ();      </ script>   </body>  </html>
  • 13. Using JavaScript 4.  By including an external file   <html>    <head>      ...    </head>    <body>      ...      <script type=&quot;text/javascript&quot;             src=&quot;my_javascript_file.js&quot;>      </script>    </body>  </html
  • 14. JavaScript in Console Open Developer Tools in Chrome (right-click > Inspect Element) Test your JavaScript in the browser!
  • 15. JavaScript Syntax The <script> tag tells the browser to run JavaScript for the text included between the tags Each JavaScript line is an instruction sent to the browser. Each JavaScript line ends in a semicolon (;) You can group several lines of code together with curly braces ( { .... })
  • 16. JavaScript Variables var car = &quot;ford&quot;; var x = 0; var y; Variable can have a value associated with it The value of the variable can change during the execution The keyword &quot;var&quot; has to be used in order to declare the variable . Declaring the variable is done at the top of the script
  • 17. JavaScript Operations x = 5 + 34; y = 3 - 2; result = 200 / 40; speed = (43 * (23 + 43)) / 4; x++; // x = x + 1; y--; // y = y - 1; x+=y; // x = x + y;
  • 18. JavaScript Comments // This is a comment /*       This is a ...           two-line comment! */
  • 19. JavaScript Variable Types Numbers var x = 0;   Strings - used for representing text  var message = &quot;Please try again.&quot; var car = 'Ford' You can use either &quot;&quot; or '' Arrays - a list of values    var arr = [3, 4, 5];   var pets = new Array();
  • 20. JavaScript Strings Used for variables that hold text   You can add strings together var text = &quot;I love &quot; + &quot;mushrooms&quot;; var vegetable = &quot;kale&quot;; text = &quot;I love &quot; + vegetable;       // The value of text will be &quot;I love kale&quot;
  • 21. JavaScript Arrays var pets= new Array() ;  pets [0] =&quot;cat&quot;;  pets [1] =&quot;dog&quot;; pets [2] =&quot;fish&quot;; var pets= new Array( &quot;cat&quot;,&quot;dog&quot;,&quot;fish&quot; ) ; var pets= [ &quot;cat&quot;,&quot;dog&quot;,&quot;fish&quot; ] ;
  • 22. JavaScript Arrays var fave_pet = pets[2]  // will get &quot;fish&quot;  pets[2] = parrots; // This reassigns the value var pet_count = pets.length; // Returns 3
  • 23. JavaScript Conditionals if (condition) {   // code will run if condition is true }
  • 24. JavaScript Conditionals if (condition){   // code will run if condition is true } else {   // code will run if condition is false }
  • 25. JavaScript Conditionals if (condition1) {     // code will run if condition1 is true } else if (condition2) {     // code will run if condition2 is true } else {   // code will run if neither condition1 nor condition2 is true }
  • 26. JavaScript Conditionals x == 4    // is value of x equal to number 4? x != 4     // is value of x different from number 4? x >  4     // is value of x greater than number 4? x < 4      // is value of x smaller than 4? x  >=   4   // is value of x greater or equal to number 4? x  <=  4    // is value of x smaller or equal to 4? if (x  ==  4) {     ......  }
  • 27. JavaScript For Loops for (var i=0; i<5; i++) {     // lines of code to run for each element   // i = 0 first time loop runs   // i = 1 second time, i = 2 the third time   .....  } Declare a variable named &quot;i&quot;  Assign the value of 0 to variable i Set the upper limit for i. In this case, i cannot exceed 5. i++ increments i by 1 each time the loop runs
  • 28. JavaScript Alert alert (&quot;Hey there!&quot;);
  • 29. JavaScript Functions A function contains a block of code that will only run when the function is called Declaring a function: function someFunction (variable1, variable2,..) {     // Code in here will run }
  • 30. JavaScript Functions To call a function: someFunction (value1, value2, ...);     // declared by us alert (&quot;Hello world!&quot;);     // function native to JavaScript
  • 31. JavaScript Functions <html> <head> <script type=&quot;text/javascript&quot;> function displayMessage() {   alert(&quot;Confirmed!&quot;); } </script> </head> <body> <a href=&quot;#&quot; onclick=&quot;displayMessage()&quot; >Click me</a> </body> </html>
  • 32. JavaScript Events JavaScript detects when a certain action is taking place. Examples of actions detected: The mouse was clicked  The mouse moves The mouse is over an HTML element User pressed a key A page has loaded A page is resizing
  • 33. JavaScript Events Mouse is over an HTML element <a href=&quot;#&quot; onmouseover=&quot;alert('You are over link!')&quot; >... User clicks on the HTML element <a href=&quot;#&quot; onclick=&quot;alert('You are clicking link!')&quot; >...
  • 34. JavaScript Events The body of the HTML has loaded: <html> <head> <script type=&quot;text/javascript&quot;> function bodyLoaded() {     alert(&quot;Page is loaded&quot;); } </script> </head> < body onload=&quot;bodyLoaded()&quot; >     .... </body> </html> 
  • 35. JavaScript and HTML <p>Once upon a time...</p> <p>What a great story!</p> document. getElementsByTagName (&quot;p&quot;);
  • 36. JavaScript and HTML <div id=&quot;title&quot;>Once upon a time...</div> document. getElementById (&quot;title&quot;);
  • 37. JavaScript and HTML <a href=&quot;#&quot; class=&quot;utility&quot;>Link</a> <a href=&quot;#&quot; class=&quot;utility&quot;>Another Link</a> document. getElementsByClassName (&quot;utility&quot;);
  • 38. JavaScript and HTML Other properties: parentNode childNodes firstChild lastChild nextSibing previousSibling
  • 39. JavaScript and HTML <div id=&quot;story&quot;>     <p id=&quot;title&quot;>Once upon a time...</p> </div>   var p = document. getElementById (&quot;title&quot;);   var parent = p.parentNode;    // will be <div id=&quot;story&quot;>
  • 40. JavaScript and HTML <p>Once upon a time...</p> <p>What a great story!</p> paragraphs = document .getElementsByTagName (&quot;p&quot;);                        //Array paragraphs.length; // Returns 2 paragraphs in array for (i=0; i<paragraphs.length; i++) {     ... }
  • 41. JavaScript and HTML document .anchors  // Array containing all links document. anchors[2] // Third link element on page (<a>) document .forms[0]  // Returns the first form HTML element document .forms // Array of all form elements on page document .images.length // How many image tags on page document .images // Array of all image elements on page
  • 42. JavaScript and HTML Get the contents of an HTML element: <html> <head> <script type=&quot;text/javascript&quot;> function getValue() {   var header_value = document.getElementById(&quot;header&quot;);   alert(header_value .innerHTML ); } </script> </head> <body>   <h1 href=&quot;#&quot; id=&quot;header&quot; onclick=&quot;getValue()&quot;>Click me!</h1> </body> </html>
  • 43. JavaScript and HTML Return the class name of an HTML element: <div id=&quot;title&quot; class=&quot;large&quot;>Savings</div> document.getElementById(&quot;title&quot;) .className ;     // Will return &quot;large&quot;
  • 44. JavaScript and HTML Create HTML elements dynamically: var body = document.getElementsByTagName('body')[0]; var newdiv = document .createElement('div') ; newdiv .setAttribute ('id',&quot;images&quot;); newdiv .innerHTML =      &quot;Images: <img src='plant.jpg' onclick='alert('Plant!')'/>&quot;; body .appendChild(newdiv) ;
  • 45. JavaScript & HTML & CSS Change the CSS style of an HTML element: <div id=&quot;title&quot; class=&quot;large&quot;>Savings</div> document.getElementById(&quot;title&quot;) .style .fontSize = &quot;14px;&quot; element.style.color element.style.background element.style.backgroundImage element.style.backgroundPosition
  • 46. JavaScript & HTML & CSS Making an HTML element appear and disappear: <div id=&quot;title&quot; class=&quot;large&quot;>Savings</div> document.getElementById(&quot;title&quot;) .style.display = &quot;none&quot;; document.getElementById(&quot;title&quot;) .style.display  = &quot;block&quot;;
  • 47. JavaScript & HTML & CSS CSS selectors are separated by dash -  : background - image font - size In Javascript, they are camelCased : background I mage font S ize
  • 48. Debugging JavaScript var image = &quot;cat.jpg&quot;; console.log (&quot;Value of var 'image' is &quot;, image); console.log (&quot;Value of var 'image' is &quot; +image);   This prints in Developer Inspector tools Console tab:   JavaScript ERRORS are also found here.
  • 49. JavaScript Exercises Make a few boxes using CSS and HTML. When you click on them, change their color. Choose a random different color every time, use an array to store them.
  • 50. Resources Code Academy JavaScript Tutorial: http://www.codecademy.com/ W3Schools JavaScript examples: http://www.w3schools.com/js/js_ex_dom.asp
  • 51. Next Class... Assignment #5 Due Be ready to present your final project idea We'll do a recap of some of the concepts we covered Some more advanced Sinatra concepts.

Editor's Notes

  • #9: Java can stand on its own A Java &amp;quot;applet&amp;quot; is a fully contained program Java needs to be compiled before it can run on the web