The Modern Day Web Page

Sugar (HTML), spice(CSS), and everything nice (Javascript/PHP). Add a little Chemical X(nginx) and what do you have?

That’s right, you have a website! 

Most of today’s web pages are built upon a mixture of these fundamental web components. The components all work together and culminate into a nicely designed, useful web page.

Imagine you’ve rented a new suite or building and you want to turn it into your company’s storefront; what does a store need to attract and facilitate customers? You’ll want to put up a sign or banner that has the name of your company. Inside the store you’ll want to label the aisles to make it easy for your customers to find what they need. Then, you’ve got to spice up the place with a design that is pleasing to the eyes –decoration is a must for attracting new customers. Lastly, you’ll need some functions, right? A store cannot function without a process for purchasing and/or returning/exchanging goods.

How does all that about opening a store relate to web pages you ask? Well, let’s break it down.

The Building: the Web Server

The building or suite: a physical/virtual space that enables you to carry out your business. Without it, you have no store. In web development the web server is this “building/suite.” The web server is where you “host” your actual website and it allows your customers to visit and do business with your company. In my first example, the web server is nginx although there are many other web servers available for use (Node, Apache, IIS, etc.)

The web server serves up everything that your site is composed of to visitors. So, basically just HTML/CSS/JS.

Labels: HTML

Putting up a banner and labeling the aisles make your store semantic. When your customers need to find canned goods, they know to look for the canned goods isle. If your customers want to recommend your store to their social circle they’ll think back to the big banner outside with your store’s name.

HTML is a markup language. By now you’ve probably come to the conclusion that “markup language” just means that it’s a language for marking things up. ‘Tis true my dear.

HTML provides the developer the ability to label every piece on a web page using tags(elements)/classes/ids. For instance, you could label the heading on your page with <h id=”store-banner”>Rugged Bear’s Rugged Bear Rugs</h> so that whenever you need to refer to the banner you can just call it “store-banner”.

<head>
  <link href="https://fonts.googleapis.com/css?family=Bitter" rel="stylesheet">
</head>

<body>
  <div id="neon-back-light">
  <h1 id="store-banner"><span id="neon-back-light">Rugged Bear's Rugged Bear Rugs</span></h1>
  </div>
  <table id="aisles">
    <caption><h3>Store Aisles</h3></caption>
    <tbody>
      <tr>
        <th>Rugged Bear Rugs</th>
        <th>Canned Goods</th>
        <th>Fresh Produce</th>
        <th>Candy</th>
      </tr>
      <tr>
        <td>Green Bear Rug Made from not Bears</td>
        <td>Tomato Soup</td>
        <td>Lettuce</td>
        <td>Snickers</td>
      </tr>
      <tr>
        <td>Yellow Bear Rug made from not Bears</td>
        <td>Beef Ravioli</td>
        <td>Broccoli</td>
        <td>Twix</td>
      </tr>
    </tbody>
  </table>
  
  <div id="example-buttons">
    <button id="buy-button">Buy</button>
    <button id="login-button">Login</button>
  </div>

</body>

Decorations: CSS

Decorating your store is a good way to make it stand out. If your store looked like any other store,  why would a potential customer visit your store over another more visually pleasing one?

This is where the pieces of the puzzle start fitting together. CSS is a language that allows you to decorate the pieces of your web page. Adding onto the previous example with HTML, if you wanted to add a nice neon back lighting for your website’s banner then you would add the CSS to “store-banner”. This would be an extremely painful process if you had no way to refer to your banner. Thanks HTML!

body {
  background-color: black;
  color: white;
  font-family: 'Bitter', serif;
}

#store-banner{
  color: white;
  margin: 10 auto;
  text-align: center;
}

#neon-back-light{
  animation-name: neonflash;
  animation-duration: 0.5s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  margin: 0 20%;
  border-radius: 10px;
}

#aisles{
  margin: 0 auto 1.5em auto;
}

#aisles,tr, td, th{
  color: white;
  border: 1px solid white;
  border-color: white;
}

#example-buttons{
  margin: 0 auto;
  text-align: center;
}

@keyframes neonflash{
  from{
    background-color: rgb(0,0,0);
  }
  to{
    background-color: rgb(57,255,20);
  }
}

Functions: Javascript

You have your aisles labeled, and your store looks really nice. A customer walks in and realizes they can’t buy anything and walks away. Why is that? It’s because your store needs processes and functions! You have to come up with a process for your customers to purchase items in order to actually sell items, no?

You can provide functions to your web page by using Javascript. If you want your customers to be able to purchase something from your website then you have to write in a “purchasing function”/flow. If you want to give your customers the ability to log in then you’ll have to write in that function. Where will you tie those functions to? Oh yeah, good thing you labeled every piece of your web page with HTML. Bind that purchase function to your “buy-button” and your login function to the “login-button”.

let clicked = 0;

const tellEmHowItIs = (timesClicked = 0, mode = "buy") => {
  if(mode.toLowerCase() === "buy"){
            switch(timesClicked){
              case 0:
                alert("You gon need a backend for this homie.")
                break;
              case 1:
                alert("Fine. We just magically charged your credit card $100000! >_>");
                break;
              case 2:
                alert("Don't you have something better to do?");
                break;
              default:
                alert("o_o;..........");
            }
        }
  
  if(mode.toLowerCase() === "login"){
    switch(timesClicked){
           default:
              alert("Come on, come this way. This is definitely not a bear trap.");
        }
}};

document.getElementById("buy-button").addEventListener("click", () => {
  tellEmHowItIs(clicked, "BUY");
  clicked++;
})

document.getElementById("login-button").addEventListener("click", () => {
  tellEmHowItIs();
})

Yay, we did it!

Congratulations! With all these parts in place, you now have a functional store/website. Wait, you wanted a place to store your inventory and your customer’s data? Well, you need a warehouse/database/backend for that. I’m out of time though, talk to you later!

XHRs with Javascript

There aren’t many websites and web applications these days that don’t utilize XHRs. In fact, if you want to build an application or website that pulls in data from an API, chances are, you’re going to have to send XHRs to GET the data.

What is an XHR? XHR stands for XMLHttpRequest; It’s a browser-based javascript object used for requesting and transmitting data from and to a web service (API.) What sets it apart from using a standard HTTP request is that it is asynchronous and will not force a page reload.

What this allows us to do is change elements on a web page without the browser reloading/refreshing. This is very useful for bringing the “Desktop App”-like experience to the web. *Cough* Single Page Apps

Making use of XHRs

When it comes to using XHRs , there are many roads you can take. Let’s talk about a few of the more widely used implements. Here, have some Chuck Norris jokes too.

A code pen presenting the many methods you can use to take advantage of XHRs

XMLHttpRequest

This is the oldest implementation of XHR out of all the methods I’ll discuss in this post. It’s native to the browser and has the highest compatibility out of all the other methods.

In exchange for the compatibility it’s also one of the more painful ones to read as you’ve got to remember the readystate states. I suppose one way around this is to use an enum to map the states to something more semantic. Still, it’s a bit bulky and it doesn’t support the use of service workers.

//Utilizing XHRs

//Initializing the XHR javascript object
const XHR = new XMLHttpRequest();

    //There are 5 states an XHR object can be in
    /*
    0 - Unsent : open() hasn't been called yet
    1 - Opened: open() has been called
    2 - Headers Received: send() has been called and headers and status can be veiewed
    3 - Downloading: responseText holds partial data
    4 - Done: operation is complete
    */

//The lowercase actually matters here on this function
XHR.onreadystatechange = () => {
    /*
    //This uncommented line below effectively states that if the XHR request is done and the response was OK (status 200) then send a toast/alert with the chuck norris joke from the 
    chuck norris jokes API.
    
    The responseText property holds the response from the XHR request in stringified form. 
    The function JSON.parse() parses the stringified JSON and turns it into a javascript object 
    We then send an alert with the data stored inside of object's value.joke property
    
    */
    (XHR.readyState == 4 && XHR.status == 200) ? alert(JSON.parse(XHR.responseText).value.joke) : null;
}  
    //These two lines "open" the XHR object and configures it to send to the designated endpoint (second parameter) and then calls the send() method to actually send the XHR off
XHR.open("GET", "https://api.icndb.com/jokes/random");
XHR.send();


//Try running this in Chrome Developer Console --> Press F12 go to console, copy and paste this code
//This will only work in a Browser that implements alert

Fetch

This is the more modern implementation of XMLHttpRequest. Fetch is also native to the browser; this means that you do not have to import anything with respects to the front end when developing for web browsers.

The readability of Fetch is heaps and bounds above its predecessor. And, with a slew of other benefits (such as the ability to use service workers) this is the one I prefer to use.

As Fetch is younger, the compatibility is just a tiny bit less robust. IE isn’t supported at all, for example–and that can be a deal breaker for many. To get around this you can always use a polyfill.

//Utilizing the Fetch API to send XHRs

//Because fetch is native to the browser you can just call it

//The default behavior for Fetch is to GET
fetch("https://api.icndb.com/jokes/random")
    .then(res => res.ok? res.json() //Because Fetch does not throw an error on status messages we do our own manual check here
        .then(parsedData => alert(parsedData.value.joke)) //parse the data and output the joke via alert
        : 
        alert(<code>Something was wrong with the request: ${res.status}:${res.statusText} </code>)  //Send an alert/toast describing what the response status was
    )
    .catch((err) => alert(err)) //Fetch will throw an error with errors other than HTTP status errors (like if you don't have a network connection)

Axios

Axios is a third party library that is made specifically for web requests. Because it is a third party library, you must import/include it before using it unlike the previous two.

In general the Axios syntax is a lot easier on the eyes. This is true especially when you start to tack on request options.

One of the benefits of using Axios is that you don’t have to parse responses coming in, it’s just automatically ready to be used as a Javascript object. 

Another major advantage of Axios is that the .catch() block will actually catch HTTP status 400 errors unlike Fetch where you have to implement your own HTTP status 400 error catching.

//Utilizing Axios to send XHRs

//axios is not native to the browser environment so you would have to include it via html script tag

axios.get("https://api.icndb.com/jokes/random")
    .then(res => alert(res.data.value.joke)) //send alert with the joke
    .catch(err => alert(<code>There was a problem with the request! ${err.response.status}: ${err.message}</code>)) //response status 400s are actually caught in the catch block of Axios. Neat!

Decisions…decisions..

All in all, each implementation of XHR has its benefits and its drawbacks. Similar to how a pen is just a tool to a writer, it is up to the developer to analyze and assess which method would be the ideal choice for a project.

Do you want your code to be as legible as it can be? Maybe Axios is your cup of tea.

Want to strike a balance between how clean your code base is, while limiting the usage of third party libraries? Fetch, boy!

Looking to break free from the chains that bind? Raw, primeval, untamed and unchained: these are the adjectives that describe the person who wishes to use the default XHR API. Perhaps your target platform is Internet Explorer 8. Then, naked XHRs will set you free my guy/gal. (We don’t judge around here.)

The power is in your hands. Choose wisely, or not. *shrugs*

The Start of a Tech Bear’s Journey

*Waves*

It was about 8 years ago now that I started on my programming journey. A majority of my time was spent gaming. And the majority of my gaming time was spent on Minecraft Beta 1.6 (Java Edition). At that point there wasn’t really even a concept of a “Java” edition. It was just Minecraft.

Though I didn’t know how programming languages worked nor did I understand any of the lingo, I knew one thing– Minecraft was written in Java. Afterall no one would stop talking about how bad Java was hindering the game.

Still, the idea of creating or modifying a piece of software to fit my idea of awesomeness was alluring

One day, while having a Minecraft session with my close group of friends I had an idea: Luminescent dirt (I’ll come back to this later). What prompted the idea was the fact that at the time there weren’t any sources of “light” in the game that you could place wherever you wanted except a torch.

The drawbacks of the torch were many. One: the torch itself was a very ugly design. Two: the torch would take up an entire “block” so it would limit the type of designs you could create. Three: the warm lighting made everything seem so dark in an already murky game.

“How could I build a dirt castle that was well lit yet wasn’t full of ugly torches?” This was a thought that permeated my brain space for the better half of a month.  I’d heard of video game “modding”, so I decided to ask the google lords.

Java Java Java. That’s what I needed to know. I was excited. If I learned Java I could make dirt shine, the sky green, and the world mine. So I set off to learn Java. 

I bought a Headfirst Java book from O’reilly; I locked myself in a closet with no internet connection and blazed through the book in a record two weeks. Then, I found a Java course online by Bucky a.k.a. thenewboston on YouTube and went through his course. By the end: I’d gained knowledge about OOP, IDEs, Polymorphism, and various programming topics.

I took to Minecraft equipped with my newfound knowledge and confidence. I immediately hit a brick wall. “How do I actually add code to an existing application?” Ultimately, most of the exercises I had done were written by me and only me. I took to the forums once more.

Aha!–I found a modding guide. There wasn’t much in the way of actually modding things in the game. However, the author of the guide spoke of things such as deobfuscators and decompilers. They also spoke of certain resources that were used in the game. Things like a bitmap file that had all the textures for the objects within the game. Everything clicked in my mind and a giant light bulb appeared above my bear brain and lit up.

I deobfuscated and decompiled the minecraft.jar file, and with my Java knowledge I began to read over all the classes and functions to understand how everything worked together. What I learned about Interfaces and Inheritance came together in a crescendo and the puzzle pieced itself together.

I began adding new items into the game. I added “tornado” blocks that sent you flying miles into the air, “quicksand” that would slowly eat you up and chip away at your life bar, swords that sent out explosive fireballs and even explosive kunais that would tick down a timer and explode (Naruto fans will like this one.)

It was then that I saw the power of programming–that is, the power to change worlds.

And then, finally. I made dirt that shined.