web

How to use jQuery and Handlebars in your website

jQuery and Handlebars are both external to the core functionality of JavaScript. Both are libraries that we can use and include when making websites. Doing so is very simple. We include <script>s in the head of our HTML file, as in the following example:

<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/handlebars@latest/dist/handlebars.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  </head>
  <body>
    <h1>Hello, World</h1>
  </body>
</html>

Note that jQuery is now modular, so you may want to consider whether you want to include the entire library. Above I chose to download it from Google's CDN, but there are other options listed here.

Using APIs to make things happen on the web

Towards the end of the Launch School core syllabus, we start to work with API calls. These allow us to pass information between servers. It turns out, this is really useful to be able to do.

Many if not most of the things you do online involve API calls. That little widget on the side of the web page that shows today's weather: an API call. Even things like Siri which aren't exactly web pages: using API calls.

Being able to make those calls and to interact with the services available through the internet gives users all sorts of power. The creativity comes, then, in how these things are all combined together.

That said, the dreams of the connected web have been somewhat oversold in the past. What are we on now, web 4.0?.

From a technical perspective, in this part of the course I enjoyed seeing how simple text in the form of JSON objects was behind so much of our communication and interactivity online these days. (All the more reason to have more secure ways of exchanging those plain text data).

I was also conscious of how much creativity and widened thinking has gone into expanding the possibilities of what HTML, CSS and a bit of JavaScript can do over the internet. Some of these capabilities mean that we're straining the possibilities in this area or that, but above all I take away some inspiration in how people made do with what they had instead of feeling like they needed to reinvent the wheel.

Different ways of accessing the text contents of DOM nodes in JavaScript

For a while now while studying the DOM and JavaScript's interaction with the web browser, I've been wondering about various properties available on nodes that relate to their contents or text values.

I spent a bit of time the other day unpacking what each of those do. This is a bit of code you can play around with to evaluate how they all work:

<!doctype html>
<html lang="en-US">
  <head>
    <title>title</title>
    <meta charset="UTF-8">
  </head>

  <body>
    <div>
      <h1>First bit of text.</h1>
      <p>Some <span>old</span> text</p>
      <textArea>pre-written</textArea>
    </div>
  <script>
    console.log(document.querySelector('div').innerHTML);
  </script>
  </body>
</html>
  • .textContent

This property concatenates the text of the element plus all child elements including any whitespace (from the HTML markup itself).

  • .data

This is the text content of a text node.

  • .nodeValue

For text nodes, this is the text content of that text node (i.e the same as .data). For comments, it is the comment content. For element nodes (and most other types), it is null.

  • .value

This is either an attribute property on an element (in which case the value is whatever was assigned to that property), or it is the contents of a textArea element.

  • .innerText

This is the rendered text (i.e. as displayed on the browser page) of the node and child nodes. Note, if it is not being rendered, then this will be identical to the .textContent property.

  • .innerHTML

This is all the HTML markup contained within the element (including nested elements and text (and whitespace). Note that sometimes this will just be plain text. Note too, that this can be used to create HTML inside an element as well.

Using CSS selectors with JavaScript DOM methods

I've been using JavaScript methods that interact with the DOM all week as part of my Launch School course. Among them, document.querySelector() and document.querySelectorAll() seem really useful. I realised I didn't fully understand the selectors that you were supposed to pass in as an argument, so I'm writing up some of what I discovered about them here. (See here for the documentation).

The simple part is when you have a single selector. Three important selectors are:

  • id selectors (#) — so if the html contains an id attribute of 'some-id', then you could write #some-id.
  • class selectors (.) — so if the class was 'special-style', then you can write .special-style.
  • tag selectors — for these, you just write the page itself

When combining tags, there is a complex set of options depending on whether things are siblings or descendants or children. For the most part that is TMI. The important ones to remember are:

  • descendant selector combinations — place a space between elements — so if you want to select all <div> tags that descend (however far down in the tree) from a <p> tag, then you can write p div.
  • child selector — place a > between the elements — this allows you to find elements that are direct children (i.e. no intermediary levels) of other elements. p > div will find all div elements that are the direct children of paragraph elements.

(For a more complete exploration of this topic, specifically the combination of selectors, read this blogpost.)

PhD Tools: Save your web links with Pinboard

[This is part of a series on the tools I used to write my PhD. Check out the other parts here.]

Pinboard is the successor to Delicious and various other social link repositories. It's a service I get a lot of use out of because some parts of following the news, monitoring various government / non-governmental sites etc means reading lots of small articles each day. If, in the future, I want to return to a particular article, I generally don't want to have to go through the hassle of searching for it afresh (sidebar: use DuckDuckGo instead of Google! It's great!) so I just click a button to save a page in Pinboard when I think there's a chance I'll find this useful or I want to preserve it in some way. (Visit the 'tour' part of the site to learn more about how Pinboard works.)

Pinboard also auto-adds links, if I've starred, retweeted or saved anything in Twitter. Also if I've added a link from twitter or elsewhere into my Instapaper (Pocket is also used by some people, and is supported by Pinboard), then these articles are also autosaved into Pinboard. I reason that if I've taken the trouble to save it for reading later, then there was probably something in there that I might find useful in the future, or something that I might want to reference later.

The great thing about searching your repository of pinboard links is that you can do in-text searches. So you're not just searching the name and URL of the link, but you're searching the full text of the page. This is really useful, especially if you have many links saved. I just checked my account stats and see that I have over 60,000 bookmarks saved in my pinboard account. This is over 15 years worth of bookmarking.

Pinboard also offers a paid upgrade service where it will archive copies of a page and store that archived image. That way, even if the site is later taken down, or someone deletes the page, or anything at all happens to the page, then you still have a copy of the page and can search in it, can download it etc. Needless to say, this is really useful for monitoring the Taliban's websites, for example, which are frequently targeted in take-down attempts and where data is periodically deleted from servers or changed in various ways. I often double down and make a manual archive copy of important messages/pages to be stored in DevonThink, but I generally rely on Pinboard to handle the bulk of this work for me.

The creator of Pinboard, Maciej Cegłowski, is a smart guy and who writes interesting things, and the service reflects this. (Check out his interview on the Longform podcast). It's a paid service, but is relatively inexpensive as far as these services go. Moreover, the paid nature of the service means that there's relatively little (if at all?) creep factor to using it. Pinboard isn't selling your link database on to anyone else, they aren't marketing data profiles of their users etc. It's a solid service.

The interface is pretty minimal, which I like, but in case it's not to your taste you can browse your links in any one of a dozen or so apps which can hook up into your Pinboard data. Links are fully taggable, including tag nesting etc, so you're fully covered on that front. There's a social element to pinboard that I don't use much (mainly because I don't know many others who use pinboard for saving links) but I can see that that might be a useful feature if you have a community working on a particular topic or area.

Pinboard is easy to use, reliable and relatively inexpensive. It can save you time and help you find things you read on the web. Check it out here.