function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
FSamorganFSamorgan 

CDATA not being procesed correctly

I have a visualforce page with this code:
<apex:page contentType="application/javascript">
<![CDATA[
for (var i=0; i < 5; i++) {
  console.log('Hello World');
}
]]>
</apex:page>

When I load the page up I get this:
<![CDATA[ for (var i=0; i < 5; i++) {]]><![CDATA[   console.log('Hello World');]]><![CDATA[ }]]><![CDATA[ ]]>

This is obviously incorrect and a bug. The content type is application/javascript and the output is outputting XML. The output should be the CDATA contents.
pconpcon
This is odd, I was unable to get it to work in anyform.  In order to get this actually looked at as a bug you will need to contact Salesforce support directly.  Personally I would not use Visualforce to generate your javascript files because of the additional overhead. I would put them in a static resource and reference them that way.
FSamorganFSamorgan
The reason why I need it to be in a Visualforce page is it is being requested from a Web Worker and I need dynamic values to be filled in. Visualforce Pages seem to have trouble with content type Javascript since the source code often contains < and &. CDATA would solve this issue except that SF is doing something odd/wrong with it so I wanted to bring this to the attention of the community.
FSamorganFSamorgan
Another bug I just noticed is that my carriage returns are being replaced with a space. This also is wrong CDATA handling.
FSamorganFSamorgan
For now while Salsforce is fixing their bug I am going to use a workaround I came up with. Instead of the CDATA tags I will use //<script> at the top and //</script> at the bottom.