Javascript try/catch does not supress ColdFusion error message

Posted At : September 5, 2008 8:09 AM | Posted By : Dave
Related Categories: ColdFusion 8,Javascript

While working with some ajax stuff this morning I can across an interesting issue. I have a dynamic form where fields could or could not be defined based on user action. So the function that processed the form was nimble in the fact that it could account for missing fields. Well, it was supposed to be. I kept getting an error message alert that said a field did not exist. I found this strange since the try/catch should have prevented the alert. Here is a strip down of the code I was using:

<script language="javascript">   
   try {
      t = ColdFusion.getElementValue('user_eml');
   } catch (err){
      t = '';
   }
</script>

Since the form field with an id of "user_eml" did not exist ColdFusion.getElementValue threw an alert that the field did not exist. The try/catch did nothing to suppress the alert. I ended up changing the code to use getElementById and the try/catch worked as expected.

Till next time,

--Dave

Comments
Maybe it's because you were testing for a value of a presumed to exist field? It would make sense that it started working once you changed the code to test for the existence of the element ID instead. It could be that the try was breaking on trying to get a value from a non-existent field and the catch was not "catching" it, but that sort of seems to defeat the purpose doesn't it.

However, I'm not a Javascript expert by any means; just a thought...
# Posted By Anthony Hixon, Jr. | 9/5/08 9:04 AM