Thursday, November 24, 2011

 

Why use href="#"?

Yes, I finally figured why people so often use construct

<A href="#" onclick=do something...

whereas I have always been using

<A href="javascript:foo()" ...

Let's look at this test file:
<html>
<script>
function foo(elm) {
  if (elm == window)
    alert('received Window!');
  else
    alert("received <" + elm.tagName + ">");
}
</script>
<A href="javascript:foo(this)">Click 1</A><br />
<A href="#" onclick="foo(this); return false">Click 2</A>
</html>
Second click works as "expected," passing relevant "<A>" node into Javascript function, but first one does not; instead; 'this' is resolved to it's "default" value as current document "window". Also, this behavior seems to be consistent across all major browsers.


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?