Monday, April 07, 2008

More JavaScript, More "duh"

Another JavaScript "duh" moment. (You can see my previous JavaScript "duh" moment from last week here: ). A lot of times I want links to fire off JavaScript events, but to still look / taste / feel like links. Previously I have done it like this:

<a href="#" onClick="ColdFusion.Window.create('etc', 'etc.cfm');">
The upshot to this technique is that you get a link that looks and acts like a link. The minus is that when you click it, it appends a hash mark (#) to the end of your url. Not a big deal, but still.

On Friday, at the fabulous Salt Lake RIA Dev Shed, I learned another little "duh" JavaScript technique. Rather than trying to explain it, I will just show. You can do the same thing as the above like this:

<a href="javascript:ColdFusion.Window.create('etc', 'etc.cfm';">

Huzzah for cheap little JavaScript tricks!

1 comment:

Duncan said...

It'd be better to append 'return false' on your onclick attribute. You could even then use a normal link in the href attribute, if you wanted to provide an alternative that would work for anyone with javascript disabled. The return false prevents the browser going there in the first place, if the javascript is enabled (you probably already know all this, just spelling it out for the benefit of others)