TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The Single Piece of JavaScript on HN

272 点作者 qasar大约 9 年前

25 条评论

cypherpunks01大约 9 年前
<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 未加载
Illniyar大约 9 年前
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 未加载
gedrap大约 9 年前
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 未加载
smadge大约 9 年前
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 未加载
mwcampbell大约 9 年前
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 未加载
iamleppert大约 9 年前
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 未加载
mdavidn大约 9 年前
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 未加载
seanalltogether大约 9 年前
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 未加载
meesles大约 9 年前
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 未加载
mchahn大约 9 年前
So without JS enabled you can&#x27;t vote?
评论 #11308076 未加载
评论 #11308223 未加载
评论 #11308114 未加载
评论 #11308065 未加载
z3t4大约 9 年前
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 未加载
amelius大约 9 年前
Now if only I could convince my UX designer that websites without much JavaScript are cool.
评论 #11312919 未加载
teddyh大约 9 年前
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 未加载
sillysaurus3大约 9 年前
Little-known fact? Man, this place really got big. :)
评论 #11308054 未加载
lucb1e大约 9 年前
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.
zbruhnke大约 9 年前
and yes Author is right ... you need 500+ Karma to downvote
评论 #11319452 未加载
cpg大约 9 年前
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 未加载
pmlnr大约 9 年前
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> )
wrigby大约 9 年前
When you&#x27;re not logged in, the `onclick` attributes aren&#x27;t present in the anchor tags.
评论 #11308254 未加载
rawTruthHurts大约 9 年前
Well, if that&#x27;s all you need, that&#x27;s all you need.
kentor大约 9 年前
He exposed his auth id
评论 #11308161 未加载
hayksaakian大约 9 年前
thanks, i learned how to use the chrome JS debugger today
razwall大约 9 年前
CSRF anyone?
评论 #11310465 未加载
plugnburn大约 9 年前
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.
marknadal大约 9 年前
Why was this post taken off the homepage? EDIT: Now it has been restored, odd.