Making with Code

Javascript #

In this lab, it is up to you to recreate our website! Through the process you will practice HTML/CSS and learn a bit of Javascript.


[0] Set up #

๐Ÿ’ป Start by cloning your starter code. Be sure to change yourgithubusername to your actual Github username.

cd ~/desktop/making_with_code/unit04_webdesign
git clone https://github.com/the-isf-academy/lab_javascript_yourgithubusername

๐Ÿ’ป cd into the lab

cd lab_javascript_yourgithubusername

๐Ÿ’ป Start by openning the repo in VS code and running the live server on the index.html page.

code .

[0] Structure of Website #

๐Ÿ’ป Start by creating the layout of the site. It should have:

  • header banner with “Making with Code”
  • sidebar with links
  • main content
  • footer <footer>

At this stage, it should just be able the layout, focusing on the placement of content and creating the 2 main columns.

๐Ÿ’ป Create the functionality of the navigation bar and link it to a new page. To create a new page, just create a new html file. The href should just point to the new filename.


[1] Re-usable HTML Elements #

Now that you have a working site and two pages, you may be realizing that there’s components of the page that are re-used. Like the header banner, side navigation bar, and the footer.

We can use javascript to create custom HTML elements, so we don’t have to copy/paste code.

๐Ÿ“– Here is an example of creating a custom HTML element in script.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class SiteHeader extends HTMLElement {
  connectedCallback() {
    this.innerHTML = `
      <div class="header">
        <h1><a href="/">Making with Code</a></h1>
      </div>
    `;
  }
}

customElements.define('site-header', SiteHeader);

You do NOT need to understand each line. Just understand these pieces:

  • line 1 and 11 - SiteHeader is the custom name in Javascript. It should have a capital first letter.
  • lines 4-7 - the string inside this.innerHTML is where you define your custom element
  • line 11 - defines a new HTML element site-header. It must be all lowercase.

๐Ÿ“– You can then use your site-header element in the HTML files.

<site-header></site-header>

๐Ÿ’ป Create new Javascript Classes for the Header, Footer, and Navigation Sidebar. Then, add them to your 2 pages.


A common feature of websites is to highlight the current page in the navigation.

๐Ÿ’ป Add this code snippet within your class SiteNavbar in script.js

const currentPath = window.location.pathname;
    this.querySelectorAll('a').forEach((link) => {
        if (link.getAttribute('href') === currentPath) {
            link.classList.add('active');
      }
    });

Let’s unpack what the different parts of it mean using the javascript console. Just like in Python, it is super useful to print out information in Javascript. For that, we use console.log().

For example:

console.log(currentPath)

๐Ÿ’ป To open the log, right click the site and select Inspect > Console.

๐Ÿ’ป Refresh the page and look at the log. This is the path of the page you are on. Try going to another page. What is in the log?

/index.html

๐Ÿ’ป Use console.log() to understand how the loop is working. Where should you put these commands?

console.log(link)
console.log(link.getAttribute('href'))

๐Ÿ’ป As you click around different pages, look at the Inspect > Elements and look at how the active class gets applied to the current link within the navigation.

๐Ÿ’ป Add a new CSS rule for the active class in styles.css, so you can tell which page is active.


[2] Button Functions #

A common use of functions in Javascript is to trigger effects after a button press. On class website, we have use this for our expand boxes. We are going to recreate it!

๐Ÿ’ป Copy & Paste this button into index.html. HTML buttons have a built in attribute onclick to trigger Javscript functions.

<button onclick="expand()">โฌ‡๏ธ Click to open</button>

๐Ÿ’ป Copy & Paste this div into index.html. This will be shown or hidden when the button is clicked.

<div class="expand-div">
    <p>Here is the expanded text.</p>
</div>

๐Ÿ’ป Copy & Paste the start of the expand() function into script.js.

1
2
3
4
function expand() {
    const expandDiv = document.querySelector(".expand-div");
    console.log(expandDiv.style.display)
}
  • const is a type of Javascript variable
  • line 2 - creates a variable that stores the HTML element with the class expand-div.
  • line 3 - prints to the console of the value of its display attribute
    • when something is hidden it’s display: none, when it’s shown it’s display: block

๐Ÿ’ป Reference this is an example of conditional statements in javascript to create the toggle behavior.

const isLoggedIn = false;

if (isLoggedIn == true) {
  console.log("Welcome back!");
} else {
  console.log("Please log in.");
}

๐Ÿ’ป Change the text of the button based on the status. Reference the below example to change the text of the button. You will need to consider how you get the button.

button.textContent = "โฌ‡๏ธ Click to open";

๐Ÿ’ป Style is nicely in styles.css


[3] Deliverables #

โšกโœจ Once you've successfully completed the lab:

๐Ÿ’ป Push your code to Github.

  • git status
  • git add -A
  • git status
  • git commit -m “describe your code and your process here”

    be sure to customize this message, do not copy and paste this line

  • git push


[4] Extensions #

Add in other features from our site. For each of these, you should create a custom class so you can easily re-use them.

Aside #

๐Ÿ‘พ ๐Ÿ’ฌ Aside

Text goes inside. There is a left darker boarder.

Notice, the “deliverables” block is similar, but with different colors. Try creating custom #ids for different color schemes.

Code Blocks #

// code blocks
The background is darker. There is a rounded edge. The font changes. 

Dark Mode #

Click a button to toggle the color scheme from light mode to dark mode.