Live data from Hacker News

The Single Piece of JavaScript on HN

blog.watchandcode.com

101–110 of 195 posts

Re: The Single Piece of JavaScript on HN

#101

Earlier quoted context omitted.

You could put each of them in an . (now to hide from the abuse, and hope my karma doesn't fall below the voting threshhold for suggesting this)

I thought of that, but then the arrows don't hide immediately after you click them as on HN - they'd still be visible until the request completes. So you'd end up with the same problem: hiding both arrows when either is clicked without using JS. (I assume you mean putting each pair of arrows in an , rather than each individual arrow.)

If it's a button, it'll be in the :focused state, you can use that to style it. Or you could use a CSS transition with a very very long time in one direction on the :active state.

And of course, you can use adjacency selectors with buttons, while you can't with :visited links.

Re: The Single Piece of JavaScript on HN

#102
post #84

Using this approach, a badly written prefetch add-on might automatically vote on everything. Any request that modifies server-side state really should use POST or PUT (or, at the very least, append a CSRF token in JavaScript). I eventually find this bug on just about every project. One time a user complained that all comments would disappear from her posts. Found that an add-on was prefetching all "delete" links, vis…

Yes, this is an old problem. It came up with early versions of Rails scaffolding, which only used GET at the time. There's no excuse for deleting with GET.

Re: The Single Piece of JavaScript on HN

#103
post #40
post #10

So without JS enabled you can't vote?

I'm getting clobbered with downvotes. I need to ask less stupid questions. The problem is that I don't really have a way to know they are stupid.

Maybe people were trying to be helpful by disabling JavaScript and clicking on the down arrow next to your message in order to experimentally determine what the answer to your question was.

Re: The Single Piece of JavaScript on HN

#104

If we ever make it around to a second piece of javascript on here, can I request that it be a collapse comment function? I promise i won't ask for anything else.

I use this userscript: https://greasyfork.org/en/scripts/18066-hn-comment-trees

It also highlights new comments and collapses threads without new comments on repeat visits. HN now feels unusable without it in scenarios where I can't use a userscript :-/

Re: The Single Piece of JavaScript on HN

#105
post #88
post #69

Earlier quoted context omitted.

Ok, I"m facing an armageddon of downvotes but why the heck was this downvoted? He made a point, that while arguable, is a valid comment. Maybe the discussion of downvoting is bringing more downvotes. Jeeez. Edit: My upvote brought him back to zero, for now.

It may be because this whole discussion is off-topic and not very productive.

Seems on-topic to me. You can't call Godwin's Law when the discussion is really about Hitler himself, and you can't claim that discussing how voting works is off-topic when the discussion is about how the voting button works.

Re: The Single Piece of JavaScript on HN

#106
post #81

A few weeks ago I turned off javascript in my browser out of principle and to see if progressive enhancement was still a thing web developers cared about (hint: it's not). I was pleased that HN worked fine, and the only annoying thing was that voting caused a page reload. It's nice to see this get the attention it deserves! Developers! You probably don't need javascript to achieve 90% of your goals. It will just slow…

But they need it to keep their job :p

But they'd have more hours to bill if they bothered to make their web pages work with JavaScript disabled!

Re: The Single Piece of JavaScript on HN

#107
post #81

A few weeks ago I turned off javascript in my browser out of principle and to see if progressive enhancement was still a thing web developers cared about (hint: it's not). I was pleased that HN worked fine, and the only annoying thing was that voting caused a page reload. It's nice to see this get the attention it deserves! Developers! You probably don't need javascript to achieve 90% of your goals. It will just slow…

This is true, however javascript is a tool that can be useful if you are building something that provides an interactive experience for the user.

In such a scenario, I have to write code somewhere to provide the experience. If I am writing it on the server side I run into several issues.

It uses server resources. For example, let's say the user is looking at a large dataset. They want to sort/search/filter that dataset. Given that they already have the data in their client, it often makes sense to use their resources to sort/search/filter the data. This lightens the load on the server.

User actions suffer from latency talking to the server. Again, if the user already has all the data that they want, not talking to the server can often result in a better user experience. Imagine something like an incremental search. Such a thing might even be impossible to implement server side due to latency issues.

Implementing client side functionality in the server can add to software complexity. Things like keeping track of user state can be handled much more cleanly on the client side.

Very often it makes tremendous sense not to make a "web page" for something that needs a lot of user interaction. Instead you want a stand alone application that gets data (without presentation) from a server. Writing your application in javascript, running it in a browser, using HTML as a presentation layer and using HTTP as your communication protocol is not a stupid way of implementing such an application (and I say this having written these kinds of applications with a variety of different technologies).

If you are writing a web page that is intended to simply present data, then Javascript may very well be unnecessary. For me, though, this does not come anywhere close to 90% of my goals.

Re: The Single Piece of JavaScript on HN

#108

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. A…

The img.src trick is used by google-analytics to log metrics cross-domain as well. Pretty neat trick.

Another thing to note is the "RESTful" nature of the links embed in the page. I guess its some form of HMAC with SHA1 + Nonce that authenticates requests for the server. It differs for every href. Elegant.

  auth=311110b7ef0f268cafa8616afcbfcec8d977111e

Re: The Single Piece of JavaScript on HN

#109
post #86

Why isn't this wrapped in a react component and written with ES7 lambda functions, transpiled from JSX (using babel of course)? God I hate what has happened to the web these days.

binary is next...

https://en.wikipedia.org/wiki/WebAssembly

Re: The Single Piece of JavaScript on HN

#110

Earlier quoted context omitted.

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. A…

For the majority of cases that I've seen, these arguments are simply excuses to avoid doing real work. It's way more fun to decide whether your amazingly awesome Unicorn startup is going to use React or Angular, or Express vs. Rails, or whatever than it is to actually go through the hard work of building a customer base and making money.

Would love to see "Enterprise HN" with Node and React and Redux (with server-side rendering), and how it compares (whether horribly or awesomely).
Post reply on HN