This seems like a complicated scenario at first until I started thinking about it again.
You use ColdFusion in the Cloud, specifically in the Amazon cloud, you then add a load balancer in front of your servers, then you notice your JavaScript starting to error out in some browsers. You cannot explain it.
Then, you spent countless hours going nuts.
Here is a sample JavaScript and ColdFusion block that would break. The simple block retrieved a name from a ColdFusion function and passed it to JavaScript:
<cfoutput>
<script type="text/javascript">
var myName = '#fCallForName()#';
</script>
</cfoutput>
The above returned this in browser lets say the name was "John" :
<script type="text/javascript">
var myName = '
John';
</script>
This, in turn, throws a "unterminated string literal" exception in JavaScript because of the line break right before the name. Of course, you say, silly you, you probably doing something in CF to return a Newline character combination. Good thought! So I changed the CF side to be as simple as possible. Here is the CF code that returns an empty string; guaranteed!
<cffunction name="fCallForName" returntype="string">
<cfreturn "">
</cffunction>
So nope, empty string still produced new line in the JavaScript output. So a few brain cycles go by and
I get to thinking to check to hit servers directly (bypassing amazon ELB altogether) and see what is returned. The return this time is enlightening, there is a blank string rather than the expected empty string, but no newline.
<script type="text/javascript">
var myName = ' ';
</script>
Yeah, progress, maybe? This is different, which again it should not be. Thus, the only thing this establishes is that the Amazon load balancer (ELB) is changing the output stream somehow. Then, I also remembered an earlier blog post of mine where I outlined that CF inserts an empty space character when the function output is used directly in place in HTML context: http://boncode.blogspot.com/2009/03/cf-coldfusion-functions-and-case-of.html
... and the fog began to lift.
I immediately reassigned the function output to a variable like so:
<cfset userName = fCallForName()>
Then, used the variable to output the content of the function:
<cfset userName = fCallForName()>
<cfoutput>
<script type="text/javascript">
var myName = '#userName#';
</script>
</cfoutput>
...and bingo, everything started to work again. No more JavaScript errors. However the conclusions here are more scary then the error:
a) ColdFusion does introduce more than just a space character when function output is used in place
b) Amazon elastic load balancer makes modifications (corrections?) to the network stream and changes the output. For each in place output of function call it adds a line break.
Both a) and b) should not happen, but they do ;o(
At least now my pain could be your gain. Cloud pittfals, I guess...simple assumptions like surely the load balancer will not modify my data could throw you off .
Cheers,
B.
2 comments:
Did you try to use output="No" for cffunction and cfcomponent?
Good suggestion.
Yep, it has been tried. This is a UDF. There was no change on output="No" Output="Yes" or no output definition.
Post a Comment