Skip navigation
Currently Being Moderated

&amp is not encoding correctly in ColdFusion

May 8, 2013 7:04 AM

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

 
Replies
  • Currently Being Moderated
    May 8, 2013 7:46 AM   in reply to Abdul L Koyappayil

    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

     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2013 3:59 AM   in reply to Abdul L Koyappayil

    &amp; is correctly encoded in ColdFusion. The result you're getting is expected.

     

    Now, some explanations. Browsers do in general render &amp; as &. You should realize that the cfdump tag converts & into &amp;, 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 &amp; 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 &amp; latheef">

    <cfoutput>#x#</cfoutput><br>

    <cfdump var = "#x#">

     

    then the result sent to the browser will effectively be

     

    abdul &amp; latheef<br>

    abdul &amp;amp; latheef

     

    The browser renders this as

     

    abdul & latheef

    abdul &amp; latheef

     

    This explains why you see abdul &amp; latheef.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points