How Do Links Work

 

How Do Links Work

So you’re browsing the Internet, getting lost in the intricate labyrinth of hyperlinked information, you vaguely remember opening a Wikipedia article on the Battle of Trenton, but now - an hour later - you’ve somehow ended up binge watching videos from the weird part of Youtube. You start wondering: How did I get here?

So let’s talk about what happens when you click a link.

Jesus Gets Presents or What Would the Three Wise Men Do?

It’s just past Christmas, so let’s use a relevant analogy.

Three wise men get word that Jesus will be born soon. They all want to get the kid some presents. They know for sure that the best presents ever can be bought in the small town of Ansager (domain) in Denmark, specifically - its main market (path).

The problem, of course, is that two of the three wise men haven’t got the slightest idea where Ansager is actually located. And the third dude is nowhere to be found. So the two guys go to this fourth bloke - Jim - who has a massive book filled with spreadsheets (DNS) consisting of two columns: in the left column are names of places, in the right - their actual latitude and longitude values (IP addresses). Happily, the two wise men take the coordinates and travel to Ansager by mule (http). On their way they come across the third guy, turns out he already knew the coordinates of the town, since he’s already been there before (cache).

As soon as they enter the town, they address the nearest official - Reginald:
‘Dude, where’s the market?’

And Reginald goes:
‘Over there, guys’

So they go to the market, but before the good sales-people of the Ansager market can offer anything, the three wise men need to announce their presence.

‘Hey peeps!’

And the people respond with:
‘Hey yourselves!’

And the three men go:
‘Oh sweet, you heard us!’

(Three-way handshake, ladies and gentlemen)

Then the sales-folk of the market start pitching them their wares. After several hours of picking the best possible presents - the three wise men decide to go to a tavern to chill out for a bit. They find one on, say, Krogagervej street. As they enter the tavern they need to get the bartender’s attention very much like they did with the market folks.

‘Hey bartender!’
‘Oh hey!’
‘Neat, you’ve noticed us!’

So then they rest and make a move back home, and Jesus ends up with the best… presents… ever! The end.

You Get Your Page or What Would Chrome Do?

Say, for personal reasons, you’re against analogies. Here’s the non-analogy version:

You click on a link. In the actual html document that link is represented by an <href> tag containing the URI of the page it points to. Say, my blog page - http://ohpollux.co.uk/blog. The whole thing can be thematically split up into three parts: ‘http’ (the protocol), ‘ohpollux.co.uk’ (the domain) and ‘/blog’ (the path).

If you’ve never visited that website before - your Internet Service Provider does a DNS lookup of the domain ‘ohpollux.co.uk’ and finds that it maps to a particular IP, so ‘192.30.252.153’. If you have visited the website before - the mapping of the domain to that IP might already be stored on your machine.

Now we just need to request the page from the server. Before we can do it, we need to establish a connection to it. So, through the magic of TCP and three-way handshakes we connect. A three-way handshake works like this:

‘Hey server, let’s synchronize’
‘Hey client, ok, let’s’
‘Gotcha, you’re ok with it’

Now that a connection is up and running - an http command is sent to the server through it. Something like ‘get /blog’.

The server goes to ‘/blog’ and returns the ‘index.html’ page living under that path. If the file references separate JavaScript and/or SCSS/CSS files - additional connections are established (with three-way handshakes and everything) and files are downloaded. Then the browser renders the page in all its glory on your screen. Boom.