TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Single Piece of JavaScript on HN

272 pointsby qasarabout 9 years ago

25 comments

cypherpunks01about 9 years ago
<p><pre><code> function hide(id) { var el = document.getElementById(id); if (el) { el.style.visibility = &#x27;hidden&#x27;; } } function vote(node) { var v = node.id.split(&#x2F;_&#x2F;); var item = v[1]; hide(&#x27;up_&#x27; + item); hide(&#x27;down_&#x27; + item); var ping = new Image(); ping.src = node.href; return false; }</code></pre>
评论 #11308043 未加载
评论 #11308061 未加载
评论 #11308540 未加载
Illniyarabout 9 years ago
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&#x27;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&#x27;t want to go back to the old days where every click reloaded the page and interactivity was really only done in flash.
评论 #11311563 未加载
评论 #11310437 未加载
评论 #11311622 未加载
评论 #11310507 未加载
评论 #11310629 未加载
评论 #11310505 未加载
评论 #11312751 未加载
评论 #11312913 未加载
gedrapabout 9 years ago
Is it just me, or it sounds... cult-ish &#x2F; overly dramatic?<p>&gt;&gt; it wasn’t obvious to me that Hacker News had any JavaScript at all.<p>I guess he never votes.<p>&gt;&gt; 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&#x2F;YC is some magical rocket science.
评论 #11310638 未加载
smadgeabout 9 years ago
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&#x27;s not). I was pleased that HN worked fine, and the only annoying thing was that voting caused a page reload. It&#x27;s nice to see this get the attention it deserves!<p>Developers! You probably don&#x27;t need javascript to achieve 90% of your goals. It will just slow your page load speeds.
评论 #11309859 未加载
评论 #11310649 未加载
评论 #11313470 未加载
评论 #11309437 未加载
评论 #11309277 未加载
mwcampbellabout 9 years ago
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.
评论 #11308275 未加载
评论 #11308250 未加载
评论 #11308231 未加载
评论 #11308276 未加载
iamleppertabout 9 years ago
Why isn&#x27;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.
评论 #11310272 未加载
评论 #11309565 未加载
评论 #11309315 未加载
mdavidnabout 9 years ago
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 &quot;delete&quot; links, visible only to her.
评论 #11309702 未加载
seanalltogetherabout 9 years ago
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&#x27;t ask for anything else.
评论 #11307985 未加载
评论 #11308328 未加载
评论 #11309803 未加载
评论 #11308132 未加载
评论 #11308206 未加载
评论 #11307995 未加载
评论 #11311509 未加载
评论 #11310511 未加载
评论 #11309460 未加载
评论 #11307987 未加载
评论 #11308885 未加载
评论 #11308582 未加载
meeslesabout 9 years ago
Not much to say, really. Just some simple code.<p>What I&#x27;m curious is if someone can reiimplement this without and javascript.
评论 #11308878 未加载
评论 #11308091 未加载
评论 #11308484 未加载
评论 #11308138 未加载
评论 #11308078 未加载
评论 #11308044 未加载
评论 #11309190 未加载
mchahnabout 9 years ago
So without JS enabled you can&#x27;t vote?
评论 #11308076 未加载
评论 #11308223 未加载
评论 #11308114 未加载
评论 #11308065 未加载
z3t4about 9 years ago
Seeing the &quot;auth&quot; query. There is a chance that HN was &quot;too clever&quot; 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.
评论 #11308193 未加载
ameliusabout 9 years ago
Now if only I could convince my UX designer that websites without much JavaScript are cool.
评论 #11312919 未加载
teddyhabout 9 years ago
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:&#x2F;&#x2F;www.gnu.org&#x2F;philosophy&#x2F;javascript-trap.html#AppendixA" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;philosophy&#x2F;javascript-trap.html#Appendix...</a>
评论 #11311526 未加载
sillysaurus3about 9 years ago
Little-known fact? Man, this place really got big. :)
评论 #11308054 未加载
lucb1eabout 9 years ago
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&#x27;t disappear without Javascript. Perhaps even, but I&#x27;m not certain, they could be made to disappear using css&#x27; a:visited, loosing zero functionality and having no disadvantages.
zbruhnkeabout 9 years ago
and yes Author is right ... you need 500+ Karma to downvote
评论 #11319452 未加载
cpgabout 9 years ago
Oh the irony ..<p>&gt; Want to take your AngularJS skills to the next level? If so, check out our flagship product, Angular Course. Through 115 HD videos, you&#x27;ll learn Angular and the latest best practices as you build a fictional startup.
评论 #11310375 未加载
pmlnrabout 9 years ago
Oldschool, smart, mini javascript as progressive enhancement.<p>Everyone should learn the basics before frameworks.<p>( Also: <a href="http:&#x2F;&#x2F;vanilla-js.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;vanilla-js.com&#x2F;</a> )
wrigbyabout 9 years ago
When you&#x27;re not logged in, the `onclick` attributes aren&#x27;t present in the anchor tags.
评论 #11308254 未加载
rawTruthHurtsabout 9 years ago
Well, if that&#x27;s all you need, that&#x27;s all you need.
kentorabout 9 years ago
He exposed his auth id
评论 #11308161 未加载
hayksaakianabout 9 years ago
thanks, i learned how to use the chrome JS debugger today
razwallabout 9 years ago
CSRF anyone?
评论 #11310465 未加载
plugnburnabout 9 years ago
So yeah, let&#x27;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.
marknadalabout 9 years ago
Why was this post taken off the homepage? EDIT: Now it has been restored, odd.