Dynamic Web pages - Modifying Web Pages without reloading (p124)

JavaScript gives you the power to change a web page without the user reloading the page.




In the datepicker example, you are still using HTML and CSS to create the calendar. JavaScript adds interactivity.

Manipulating a web page using Javascript:
  1. Select an element on a page
  2. Do something with the element
    • change the property of the element
    • add new content
    • remove element (eg. pop-up bubbles disappearing when mouse off)
    • extract information from element (eg. read user input)
    • add/remove a class attribute. (eg. change appearances by assigning a different class to HTML elements)
Notes

jQuery (p107)

- Its' a JavaScript Library. It's built on JavaScript. You can combine Javascript with Jquery

- Captures incompatibility between web browsers


- Make things easy. You can often do what would of taken you multiple lines of code in JavaScript with a single line of jQuery code.

Under the jquery umbrella:




- Very popular. Large user-base, shared code, and can be extended via 3rd-party plug-ins [50 Most useful jquery plugins for frontend development][50 amazing jquery plug-ins][top-jquery-plugins]


Other javascript libraries

Notes

Getting jQuery (p107)

[Getting jQuery]

You can use jQuery hosted a Content Distribution Network (CDN). (Google, Microsoft, or jQuery.com)
 
  <script src="http://code.jquery.com/jquery-3.2.1.js"></script>

  or..

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

  or..

  <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>


The benefit of this approach is that the user may have already cached the library from browsing other sites that use the same library. This saves bandwidth and results in a speed increase.



Or, you can download the jQuery file onto your web server and included it as an external javascript file
 
<script src="../js/jquery-3.2.1.min.js"></script>

The benefit of downloading to your own site is that you don't rely on the availability of 3rd party sites (CDN) and can be used in situations without network connection.
Notes

jQuery template


<!DOCTYPE html>
<html>
<head>
<title>jQuery</title>
<script src="http://code.jquery.com/jquery-3.2.1.js"></script>

<script>
// javascript or jquery here

</script>
</head>

<body>
Hello World <br>
</body>
</html>

Notes

Understanding the Document Object Model (p117)

When the browser loads an HTML file, it creates a representation of the page called DOM: Document Object Model.

<!DOCTYPE html>
<html>
<head>
<title>Document demo</title>
</head>
<body>
<p>
This file loaded:
</p>

<div id="timestamp">
</div>
</body>
</html>



The DOM lets JavaScript communicate with and change a page's HTML.
<script type="text/javascript">
  var timestamp = document.getElementById("timestamp");
  timestamp.innerHTML = Date();
  timestamp.style.backgroundColor = "yellow";
</script>





Using Javascript/DOM, you can do many things. See [HTML DOM@W3schools]

Notes

Selecting Page Elements: The jQuery Way (p119)

Selecting with Javascript: With DOM/javascript you selected objects using functions like:
  document.getElementById();         // returns an single element
  document.getElementsByTagName();   // returns an array 
  document.getElementsByClassName(); // returns an array 


Working with the selection in Javascript: most often involves for loops.
For example, searching for elements that has <a> tag thats is of class "College" would require something like this in javascript [sample file]:
 var anchors = document.getElementsByTagName('a');

 for(var i=0; i < anchors.length; i++){
		  anchors[i].style.color = "yellow";
  }

jQuery offers a powerful and easier technique for selecting and working on a collection of elements. In fact, it can do the same thing as above in 1 line
  $('a').css("color", "red");


Selection: - CSS selectors!
// <h1> tag
h1 {
  color: blue;
  background: yellow;
}

// <em> elements nested inside the <p> element
p em {
     color: blue;
}


//  CSS selector for all members of the class "important" 
//  eg. <span class="important">
.important {
  font-weight: bold;
}


// CSS selector for any <p> element in the class "important" 
//  eg. <p class="important">
p.important {
   color: red;
}


// CSS selector for element with id "newest"
//  eg. <li id="important">
#newest {
  font-weight: bold;
}

// CSS selector for <div> element with id "newest"
//  eg. <div id="important">
div#newest {
   color: red;
}



Selection with jQuery

// <h1> tag
$('h1') 


// <em> elements nested inside the <p> element
$('p em') 

//  CSS selector for all members of the class "important" 
//  eg. <span class="important">
$('.important') 


// CSS selector for any <p> element in the class "important" 
//  eg. <p class="important">
$('p.important') 

// CSS selector for element with id "newest"
//  eg. <li id="important">
$('#newest') 


// CSS selector for <div> element with id "newest"
//  eg. <div id="important">
$('div#newest') 




Working with the selections in jQuery:
Once you select one or more elements, you use functions provided by jQuery to do things [jq_01_selection.html].

   $('h1').append(" Javascript Was Here");
   $('.statistics').css("font-style","italic");
   $('.institutions').css("color","green");
   $('#reed').html('Yooo hoooooooooo!');



Where are these functions coming from? See the jQuery API
Notes

Advanced Selectors (p122)

CSS selectors are more expressive than what we've covered in class

Notes

jQuery Filters (p124)

In addition to the selectors based on CSS, you can filter your selections. [jq_01_selection2.html]

Notes

Understanding jQuery Selections (p126)

When you select using jQuery, you don't get a traditional javascript array. You get a jQuery object.

This is a good thing - jQuery captures all the browser incompatibiities and provides you with virtually everything you could do with javascript.

In addition to masking the incompatibilities, there are two big conceptual differences between pure JavaScript (DOM) and jQuery selections that make jQuery powerful. They are:

Notes

Automatic Loops (p126)

In traditional Javascript, when you select multiple elements, you need to create a loop to go through each node to do something.
  <script>
  var anchors = document.getElementsByTagName('a');
  for(var i=0; i < anchors.length; i++){
	// go through anchors[0] - anchors[anchors.length]
	anchors[i].style.display = "none";
  }
  </script>


In jQuery, this feature is built in.
<script>
  $('a').hide(); 
</script>
Notes

Chaining Functions (p127)

When you perform several operations on a selction of elements, like the following
$('#popup').width(300);
$('#popup').height(300);
you can chain them in jQuery like this:
$('#popup').width(300).height(300);

Chaning lets you add functions one after the other. (This is possible because every jQuery function returns a jQuery object).

$('#popup').width(300).height(300).text('Hi!').fadeIn(1000);

The above code applies four jQuery functions - width(), height(), text(), and fadeIn() to the tag with an ID name of popup.
Note that chaining only works with jQuery functions.
Notes

Adding Content to a Page (p127)

Notes

Lab

Study the following page (grid.html).

Complete the functions (button_0, button_248, button_5, button_678910, so that when you click on the specified buttons, the box(es) with the number that is on the button is selected. Once selected, make it toggle and change color (hint: chaining functions).
I recommend toggle() as an action first but you may explore other functions in the jquery API, specifically the effects. See how button #1 is implemented - it uses jquery filters ('div:first') and advanced CSS selectors (eg. 'h2' + 'div').

grid.html
Notes