Copy link to clipboard
Copied
I am using "&" in a coldfusion variable and assigning this to another variable. And I dumped this variable but am not getting results as I expected instead it outputting as below.
<cfset x = "abdul & latheef">
<cfset y = x>
<cfdump var = "#x#">
<cfdump var = "#y#">
Actual Output:
x = abdul & latheef
y = abdul & latheef
Expected Output:
x = abdul & latheef
y = abdul & latheef
Any idea about this why am getting output as I explained in Actual Output: . Timely help well appreciated.
Copy link to clipboard
Copied
Why would you expect an ampersand instead of the HTML encoding? You are dumping the value of the variable, which contains the encoded version. Just as if you outputted the variable.
If you output that variable to the browser you will also get the encoded version, but *the browser* will properly decode it to the ampersand. That is the result you *should* be desiring.
Jason
Copy link to clipboard
Copied
Thanks for the replay.
So you meant to say if we are output that variable into the browser which will allways give the encoded version(that is "&"). Please correct me if I am wrong or I understood in different way.
Here I am getting different results in different environments that is , I am getting "&" in one browser in QA environment and just "&" in same browser(IE) but in development environment.
Copy link to clipboard
Copied
& is correctly encoded in ColdFusion. The result you're getting is expected.
Now, some explanations. Browsers do in general render & as &. You should realize that the cfdump tag converts & into &, whereas cfoutput leaves it intact. Hence, if you run this code,
<cfset x = "abdul & latheef">
<cfoutput>#x#</cfoutput><br>
<cfdump var = "#x#">
the result sent to the browser will effectively be
abdul & latheef<br>
abdul & latheef
The browser renders this as
abdul & latheef
abdul & latheef
which is the final result you see. However, if your starting point is this code instead
<cfset x = "abdul & latheef">
<cfoutput>#x#</cfoutput><br>
<cfdump var = "#x#">
then the result sent to the browser will effectively be
abdul & latheef<br>
abdul & latheef
The browser renders this as
abdul & latheef
abdul & latheef
This explains why you see abdul & latheef.