Skip to main content
Inspiring
May 8, 2013
Question

& is not encoding correctly in ColdFusion

  • May 8, 2013
  • 2 replies
  • 1015 views

I am using "&amp" 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 &amp latheef

y = abdul &amp 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.

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    May 17, 2013

    & 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 &amp; latheef

    The browser renders this as

    abdul & latheef

    abdul & latheef

    This explains why you see abdul & latheef.

    12Robots
    Participating Frequently
    May 8, 2013

    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

    Inspiring
    May 15, 2013

    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 "&amp"). 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 "&amp" in one browser in QA environment and just "&" in same browser(IE) but in development environment.