Live data from Hacker News

The Single Piece of JavaScript on HN

blog.watchandcode.com

11–20 of 195 posts

Re: The Single Piece of JavaScript on HN

#11

function hide(id) { var el = document.getElementById(id); if (el) { el.style.visibility = 'hidden'; } } function vote(node) { var v = node.id.split(/_/); var item = v[1]; hide('up_' + item); hide('down_' + item); var ping = new Image(); ping.src = node.href; return false; }

What's most fascinating to me about this isn't the simplicity, it's that this code has lived for so long with almost no changes.

It's a great counter-example to all the recent articles about JavaScript fatigue. It's good for us to see real examples of sites that aren't caught up in the framework of the week hype.

At the end of the day we're trying to make stuff that works. ES5 vs ES6, React vs Angular vs. Ember vs. Aurelia, Angular 1 vs Angular 2, even server rendered templates vs. single page apps - it all matters a lot less than we think.

Re: The Single Piece of JavaScript on HN

#12
post #8

Not much to say, really. Just some simple code. What I'm curious is if someone can reiimplement this without and javascript.

Without javascript you would just keep track of which comments the logged-in user voted on in the requested thread and not render those buttons.

Re: The Single Piece of JavaScript on HN

#14

function hide(id) { var el = document.getElementById(id); if (el) { el.style.visibility = 'hidden'; } } function vote(node) { var v = node.id.split(/_/); var item = v[1]; hide('up_' + item); hide('down_' + item); var ping = new Image(); ping.src = node.href; return false; }

That was a lot easier than watching an entire video.

Re: The Single Piece of JavaScript on HN

#16
post #12
post #8

Not much to say, really. Just some simple code. What I'm curious is if someone can reiimplement this without and javascript.

Without javascript you would just keep track of which comments the logged-in user voted on in the requested thread and not render those buttons.

HN has to do that anyway. The hide() and vote() functions only run on click. If you refresh the page, you'll see the response lacks anchor tags for comments/posts you've voted on. Those tags are replaced with a spacer image.

Re: The Single Piece of JavaScript on HN

#18
post #8

Not much to say, really. Just some simple code. What I'm curious is if someone can reiimplement this without and javascript.

Well there appear to be two things stopping this:

- Hiding two elements on the page (one can be hidden with the CSS :visited selector)

- Remaining with your position on the page whilst also sending a message to the server and without opening new windows.

So the only way to do the task is to send a full request to the server which would return you to the page with the buttons correctly disabled. Extra load on the server of course, and a bit of an annoying user experience, but doable - hence why this is the disabled fallback.

Re: The Single Piece of JavaScript on HN

#20
post #8

Not much to say, really. Just some simple code. What I'm curious is if someone can reiimplement this without and javascript.

If scripts are turned off, the link just navigates to /vote?etc, which redirects you back to the thread. So it works already! It doesn't bring you back to where you were- but an anchor tag can fix that.

If you don't want to leave the page, iframes still work, right? So you can set the target to an invisible iframe, and the vote will go through. The arrow doesn't disappear, but I think you might be able to handle that with CSS:

http://www.inserthtml.com/2012/04/css-click-states/

Post reply on HN