Ask a Jedi: Run a callback on every ColdFusion.navigate/AjaxLink

Emil asks:

I was wondering if there is a way to set a global callbackHandler for "Coldfusion.navigate" when using the "AjaxLink()" function. You see, I'm using sIFR and I wanted to put the "sIFR.replace()" inside the callbackHandler-function.

Unfortunately there is no built in way to always run a callback for ColdFusion.navigate. Nor is there anyway to do a callback at all for AjaxLink(). What I'd recommend is simply using a wrapper function to handle calling ColdFusion.navigate() and setting up a callback. For example: <cfajaximport /> <script> function load(url,con) { document.getElementById('loadingdiv').innerHTML = "Loading content..." ColdFusion.navigate(url,con,handleResult); }

function handleResult() { document.getElementById('loadingdiv').innerHTML = "" } </script>

<div id="somediv"></div> <div id="loadingdiv"></div>

<a href="" onclick="javaScript:load('foo.cfm?x=1','somediv');return false">x=1</a><br> <a href="" onclick="javaScript:load('foo.cfm?x=2','somediv');return false">x=2</a><br> <a href="" onclick="javaScript:load('foo.cfm?x=3','somediv');return false">x=3</a><br>

This example uses a custom function, load, that simply wraps the setting of a loading message (a bit redundant since ColdFusion.navigate will show a spiner) and calling ColdFusion.navigate with the callback.

Nice and simple I think.

Archived Comments

Comment 1 by todd sharp posted on 1/20/2009 at 8:59 PM

Just an FYI that jQuery allows you to do this listening for ajaxComplete:

http://docs.jquery.com/Ajax...

You can also listen globally for ajaxError, ajaxSend, ajaxStart, ajaxStop, ajaxSuccess.

Yet another reason to love jQuery...

Comment 2 by Elliott Sprehn posted on 1/21/2009 at 10:58 AM

Mostly just a style thing, but onclick takes a snippet of javascript, you don't actually need the the "javascript:" in there.

Also, since load() returns void you can also just onclick="return load()"

Not critical, but it makes it much more compact.

Comment 3 by Raymond Camden posted on 1/21/2009 at 5:12 PM

Thanks Elliott. I normally remember _not_ to put js: in front of my stuff in onclick - must have been asleep at the wheel there. I almost always put return false though and I knew there was a better way, but I had forgot about simply doing return x() in the onlick. Thanks!