

Show HN: How HN's upvote link works - asto

<i>Each link has an anchor tag like this:</i><p><pre><code>  &#60;a href="vote?for=4156127&#38;amp;dir=up&#38;amp;by=asto&#38;amp;auth=12345&#38;amp;whence=%6e%65%77%73" onclick="return vote(this)" id="up_4156127"&#62;
  &#60;img vspace="3" hspace="2" border="0" src="http://ycombinator.com/images/grayarrow.gif"&#62;
  &#60;/a&#62;
</code></pre>
<i>The vote function looks like this:</i><p><pre><code>  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
</code></pre>
<i>byId is getElementById. I don't know about you guys but I thought this was pretty neat!</i>
======
jgrahamc
Which part did you find neat?

~~~
asto
//ping server

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

~~~
Spoom
I'm curious; why _didn't_ they use an AJAX request?

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

~~~
ImJasonH
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.

