6 ms·
This is neat (how the up/downvote onclick handler sends the info to the server). new Image().src = el.href; Where href looks like this: vote?id=xxxxx
by pygy_ 8y ago
This is neat (how the up/downvote onclick handler sends the info to the server).
new Image().src = el.href;
Where href looks like this:
vote?id=xxxxxxxx&how=up&auth=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy&goto=item%3Fid%3Dzzzzzzzz#wwwwwwww
- perfunctory 8y agoMaybe it's neat but this is not how anyone would write it today. This is the legacy code still left from the time before ajax was invented.
- winrid 8y agoProbably. I wonder though if they are trying not to use any libraries and Image was just easier than xhr.
- TekMol 8y agoHacker News was launched 2007. XMLHttpRequest has been around for about 7 years then and using it was called Ajax for about 2 years already.
- qbonnard 8y agoNot to mention the "ajax" function in the script...
- pygy_ 8y agoThere's also an XMLHttpRequest wrapper for when they care about the response. This is used for write-only GET requests (with graceful fallback when JS is disabled since those are plain anchor elements).
- pryce 8y agoIt is strange. I wonder if it's an approach that lets duplicate vote attempts get automatically responded to by the webserver cache rather than passing through to be detected computationally at the application server level, which would also involve a DB call. Conceivably this would make the architecture more robust against vote-spamming attacks, and the approach would also provide an enhanced version of this benefit if using an external caching reverse-proxy service like CloudFlare or similar (though I believe HN does not use CloudFlare). This is complete supposition on my part.
- lixtra 8y agoIs this a GET request? What if my browser wants to peek ahead on some links?
- pygy_ 8y agoThis would indeed be problematic :-) The up/down vote buttons are plain anchor elements.
- arethuza 8y agoIf the Image is being created dynamically there won't be anything for browser to peek ahead at. Edit: I'd be more worried about repeated calls - but presumably the service being called is idempotent.
- lixtra 8y agoIt's more the side effect of upvoting the whole thread that makes me wonder. https://stackoverflow.com/questions/705782/why-shouldnt-data-be-modified-on-an-http-get-request https://stackoverflow.com/questions/705782/why-shouldnt-data...
- expertentipp 8y agoImage makes the HTTP request without appending the Image to the DOM? TIL
- haywirez 8y agoThat's how most tracking scripts operate...