Adding JavaScript to Automatically Update the Year

  1. Video - m10-01
  2. Open the template.dwt file.
  3. Scroll to the footer, and find the "year" for the copyright.
  4. Replace the "year" with this text to see how JavaScript documnet.write works:
  5. <script> document.write(2001); </script>
  6. The JavaScript 'write' method bascially writes content to the web page. But JavaScript can do more than just write content to a page, it can run little programs. Rather than just displaying the number 2001, we can have JavaScript display the sum of 2001+14. Modify your code as shown below:
  7. <script> document.write(2001+14); </script>
  8. As you can see, the sum of 2001+14, or 2015 is now shown. JavaScript has many built it tools, including the 'Date' object. Update your code again:
  9. <script> document.write(new Date().getFullYear()); </script>
  10. Using this little bit of code will always display the current year in your copyright.

jQuery Navigation Update

  1. Video - m10-02
  2. Test your website. Notice that no matter what page you select, only the Home icon is highlighted
  3. Select the home/active element in the Status bar, and remove the class 'active'
  4. In the Status bar, remove the sr current class
  5. Near the end of the template page, after the bootstrap.js, paste in this additional code:
  6. <!-- Change the active state of the navigation -->
    <script>
        $(function() { // update the li active class
            var contentURL = window.location.pathname.split( '/' );
            var endURL = contentURL[contentURL.length-1];
          $('a[href="' + endURL + '"]').closest("li").addClass('active');
          $('a[href="' + endURL + '"]').closest("li.dropdown").addClass('active');
          $('a[href="' + endURL + '"]').append( '<span class="sr-only">(current)</span>'  );
        });            
    </script>
  7. This code has two parts, the first part uses JavaScript to find out the current URL of the page you are on. To see how this works, you can optionally paste this line of code in for testing at the end of your javascript, but before the closing <script> tag.:
  8. window.alert ('The current URL is ' + endURL );
  9. You will see a message that displays the current URL.
  10. Once JavaScript "knows" the current URL, it searches the page for any links that have the same URL, and assigns the links to the 'active' class.
  11. Save All and then press F12 to Preview in a web browser
  12. Close the template.dwt

Adding the jQuery Fancybox / Homepage / Gallery

  1. Video - m10-03
  2. Open the template.dwt
  3. We will be adding the Fancybox lightbox to the website. More about fancybox.
  4. While we will not be using Fancybox on every page, this is such a useful tool, we will add it to the template so the code is available when ever we decide to use Fancybox.
  5. Copy the fancybox folder in the 'tutorial/images' folder to the 'images' folder
  6. Copy the fancybox.css file to the css folder
  7. Copy the fancybox.js file to the js folder
  8. As a general rules of thumb, css files are added to the head of a web page, and javascript files are added to the end of a web page.
  9. Drag the fancybox.css file from the css folder to the css file links area in the head of the template file. The fancybox.css file should go after the font awesome cdn css  file.
  10. Drag the fancybox.js file from the js folder in Dreamweaver to the script region at the end of the body of the template file.
  11. Paste this javascript to initialize fancybox (this code is also in the examples/text_files.txt):
    <script type="text/javascript">
    	$(document).ready(function() {
    	$('.fancybox').fancybox();
    	});
    </script> 
  12. Save all
  13. Update All pages
  14. Close template.dwt
  15. Video - m10-04
  16. Open the index.html home page.
  17. First we will link an image to a word.
  18. Highlight a word in your opening text that can link to an image that you have. In this example, I will highlight the words "the humble pencil."
  19. In Property inspector, in the Link attribute, use the "browse for file" icon to locate the image you want to link to. In this case, I selected 'pencil-gallery_four-pencils from the gallery folder
  20. In the Property inspector, enter an appropriate title for the image. In this case, I will use "The Humble Pencil."
  21. Set "target=_blank"
  22. Press F12 to prompt you to save your work, and preview in a browser
  23. Save Changes
  24. In the web browser click on the words you just added an image link to. Your browser should open up that image in a new window.
  25. Return to Dreamweaver.
  26. Use the Property inspector to assign the anchor tag to the class of "fancybox." You will see many variations on the "fancybox" class, but you want the basic "fancybox".
  27. Press F12 to prompt you to save your work, and preview in a browser.
  28. Click on the words you linked to again. Your browser should open up that image in the center of the page, without leaving the website.
  29. Save All
  30. Open the gallery.html page
  31. Be sure that you have linked every image to itself, and provided a "alt" and "title" for the image. This was covered in module 6, creating the gallery page.
  32. You will also need to complete the above steps for a Fancybox / Lightbox / Homepage first.
  33. Switch to Code view.
  34. As you may recall, all the images on this page have something in common, they can be targeted using the css selected #photoGallery img. We can use jQuery to target all theses images and apply the necessary classes and attributes without typing all the code ourselves. Add the following script at the end of the javascript section, in the editable refion "addScript":
    <!-- jQuery to add fancybox functions -->
    <script>
    	$(document).ready(function() {
    		$("#photoGallery img").each( function() {
    			$(this).parent('a').attr('class', 'fancybox');
    			$(this).parent('a').attr('data-fancybox-group', 'gallery');
    			$(this).parent('a').attr('title', this.title);
    		});
    	});
    </script>
  35. Press F12 to prompt you to save your work, and preview in a browser
  36. Clicking on the image should open up a large image, on the same page.

Adding the jQuery Audio Player

  1. Video - m10-05
  2. Open the media.html page
  3. We will be adding the audioplayer jQuery to the media gallery page. More about audio player
  4. Switch to Code view.
  5. Copy the audioplayer.css file to the css folder
  6. Copy the audioplayer.js file to the js folder
  7. As a general rule of thumb, css files are added to the head of a web page, and javascript files are added to the end of a web page.
  8. Drag the audioplayer.css file from the css folder to the editable region in the head of the media.html file.
  9. Drag the audioplayer.js file from the js folder to the addScript editable region at the bottom of the page.
  10. Paste this javascript to initialize audio player:
    <script>
    	$( function() { $( 'audio' ).audioPlayer(); } );
    </script>
  11. Press F12 to prompt you to save your work, and preview in a web browser
  12. You will notice that the native HTML5 audio player has been replaced. This new jQuery audio player will look the same, and work the same, on any modern web browser.
  13. Save All

Wayne Joness
West Los Angeles College, cs952
Fall