Hello Friend Web Version

Your Hello.js file can now filter out friends and enemies and respond accordingly. Awesome!

Unfortunately it only exists on your local machine, and onle runs in the terminal.

Let's share it with the world!

Structuring your directory

Make a new subdirectory for your hello.js program and go ahead and move that file into that directory.

Inside that same directory create a new file calledindex.html

In a blank HTML file you can type ! and then hittab to get a basic HTML template.

Serving your Files

We are going to use a javascript library called node-static to serve your static files so we can view them in the browser.

Adding Content

Your browser probably just opened up to a blank page. This is because we haven't added any content yet, but that's easy to fix.

Create a <div> element with an id property and add a little text content.

Your index.html file should now look something like this

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <div id="myID">Hello!</div>

Importing your code

To get your code into your web page you will need to add a <script> tag to the bottom of the <body> section of your HTML and give it a property of src="hello.js"


  <script src="hello.js"></script>

Congratulations! Your code will now run in the browser

Changing Input/Output

Your code is still writing to the terminal, but we want it to greet you on the page.

Instead of using console.log() as your output lets target your <div> and overwrite its text content (yourDiv.textcontent=...) with your friend/enemy message.

Getting the Name

Now we are going to get the name to be passed into your hello.js file from the URL

If the user types localhost:8080/name into the browser we want the page to respond to that name.

Let's console.log out window.location.pathname

What did you get?

Put it all Together

Once we get the name we can pass it into the program

Use your Javascript file to manipulate the DOM to display the desired message for the given name

Restart your server and test it out!

You may need to do a hard refresh of the page before your changes take effect. Ctrl + shift + r to do a hard refresh on a PC; cmd + shift + r on Mac

 Previous Next