Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Show HN: How HN's upvote link works
11 points by asto on June 25, 2012 | hide | past | favorite | 6 comments
Each link has an anchor tag like this:

  <a href="vote?for=4156127&amp;dir=up&amp;by=asto&amp;auth=12345&amp;whence=%6e%65%77%73" onclick="return vote(this)" id="up_4156127">
  <img vspace="3" hspace="2" border="0" src="http://ycombinator.com/images/grayarrow.gif">
  </a>
The vote function looks like this:

  function vote(node) {
  var v = node.id.split(/_/);   // {'up', '123'}
  var item = v[1]; 

  // hide arrows
  byId('up_'   + item).style.visibility = 'hidden';
  byId('down_' + item).style.visibility = 'hidden';

  // ping server
  var ping = new Image();
  ping.src = node.href;

  return false; // cancel browser nav
byId is getElementById. I don't know about you guys but I thought this was pretty neat!


Which part did you find neat?


//ping server


It's an interesting way to not use XMLHttpRequest for Ajax.


I'm curious; why didn't they use an AJAX request?


Backward compatibility with really old browsers is all I can think of.


It's also much less code. It only really works if you don't care about the content of the response, though you could add an onerror handler that would fire on all error cases.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: