3 Replies Latest reply: May 17, 2013 3:59 AM by BKBK RSS

    &amp is not encoding correctly in ColdFusion

    Abdul L Koyappayil Community Member

      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.

        • 1. Re: &amp is not encoding correctly in ColdFusion
          12Robots Community Member

          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

          • 2. Re: &amp is not encoding correctly in ColdFusion
            Abdul L Koyappayil Community Member

            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.

            • 3. Re: &amp is not encoding correctly in ColdFusion
              BKBK MVP

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