<p><pre><code> 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;
}</code></pre>
I see a lot of comments here about how not having js on the page is a great thing and lets get back to the old days.<p>Lets be frank here, HN's UI is not good. At the very least it needs collapsible comments and responsiveness to mobile.<p>The fact that HN is a success is despite its bad UI, or maybe because of the many HN readers out there that fix all the broken stuff on the page.<p>While js on sites have surely overgrown, and there is a place for minimalism, I really don't want to go back to the old days where every click reloaded the page and interactivity was really only done in flash.
Is it just me, or it sounds... cult-ish / overly dramatic?<p>>> it wasn’t obvious to me that Hacker News had any JavaScript at all.<p>I guess he never votes.<p>>> I thought it would be weird and complicated, but it turned out to be quite the opposite. You’ll have to watch the video to see what I mean.<p>Well, it sends a HTTP request and hides an element. How on earth can it be complicated? I mean, ok, you can use The Framework Of The Week to do that and just that. Not everything that is related to HN/YC is some magical rocket science.
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!<p>Developers! You probably don't need javascript to achieve 90% of your goals. It will just slow your page load speeds.
The HTTP request for voting should be a POST request, not a GET request, since it changes state. So both the JS hack involving an image and the non-JS fallback are broken. The JS function should use XHR, and I guess the non-JS fallback should be a form with an image button, not a link.
Why isn't this wrapped in a react component and written with ES7 lambda functions, transpiled from JSX (using babel of course)?<p>God I hate what has happened to the web these days.
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).<p>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, visible only to her.
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.
Seeing the "auth" query. There is a chance that HN was "too clever" about it, and it might be possible to use other users id to upvote, or upvote many times, if you can figure out the hash algorithm.
I wish the code had a license. It is easy enough to add a license to inline Javascript so that it is machine-readable:<p><a href="https://www.gnu.org/philosophy/javascript-trap.html#AppendixA" rel="nofollow">https://www.gnu.org/philosophy/javascript-trap.html#Appendix...</a>
What I find interesting is that even this little piece could be done in plain HTML using an iframe for each vote button. The obvious disadvantage is http requests for each vote button, but even a middle ground could be chosen where the vote buttons submit to a hidden iframe and just don't disappear without Javascript. Perhaps even, but I'm not certain, they could be made to disappear using css' a:visited, loosing zero functionality and having no disadvantages.
Oh the irony ..<p>> Want to take your AngularJS skills to the next level? If so, check out our flagship product, Angular Course. Through 115 HD videos, you'll learn Angular and the latest best practices as you build a fictional startup.
Oldschool, smart, mini javascript as progressive enhancement.<p>Everyone should learn the basics before frameworks.<p>( Also: <a href="http://vanilla-js.com/" rel="nofollow">http://vanilla-js.com/</a> )
So yeah, let's all go back to the stone age just because of some purist morons with JS disabled.<p>In my honest opinion, the only possibility browser vendors should disable these days is the possibility to turn JS off.