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
Andy Freeston_LarterAndy Freeston_Larter 

JavaScript being corrupted when delivered through VisualForce

Hi.

 

I am experiencing the corruption of my JavaScript code when delivered through VisualForce. The code is embedded in my page rather than being held in a separate JS file.

 

The offending section of the original code looked something like this:

<apex:page showHeader="false" sidebar="false"> 
	<script type="text/javascript">
		function SMTInitCheckboxes() 	
		{
			for (var i=0;i<iMax;++i)

 

 

When this gets loaded into the browser I get a JavaScript error. Viewing the code in the browser shows that the text has been modified from my original source:

<script type="text/javascript">
  function SMTInitCheckboxes()  
  {
   for (var i=0;i<iMax className="SMTTRError" ;++i)

 

You can see that ‘className="SMTTRError"’ has magically appeared in the middle of the FOR statement. This is text from much further down the page. I assumed that I had a JavaScript bug and commented out the code but this did not fix the problem.

 

I systematically eliminated all of the VisualForce, HTML and JavaScript until I got the entire page content down to this:

<apex:page showHeader="false" sidebar="false">
<script type="text/javascript">
(i=0;i<iMax;i)
</script>
</apex:page>

The received HTML code (according to FireBug) is as follows. Notice the space between iMax and the semi-colon. The problem is still occurring but only the extra space is being added.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html><head></head><script type="text/javascript">
(i=0;i<iMax ;i)
</script></html>

 

 

I have done the following to try and eliminate the problem with no success:

  • Commented out the JavaScript.
  • Wrapped the JavaScript in CDATA sections.
  • Used different browsers (IE and FireFox).
  • Tried delivering the same content through IIS.

 

Can anyone suggest why my code is being corrupted? It looks like some kind of buffer error in the SFDC parser or page builder but I am hoping it is something that I have screwed up.

I hope to avoid having to move all of my JavaScript into separate JS files simply due to the large number of pages involved.

 

Thanks for your help.

Best Answer chosen by Admin (Salesforce Developers) 
joshbirkjoshbirk

Andy, going to suggest and actually somewhat hope this is not the case - but if it fixes it, good to know:

 

Try spacing out your expressions so that the < isn't next to other characters, so instead of:

 

for (var i=0;i<iMax;++i)

Try:

 

for ( var i = 0; i < Max; ++i) 

And see if that helps.

All Answers

joshbirkjoshbirk

Andy, going to suggest and actually somewhat hope this is not the case - but if it fixes it, good to know:

 

Try spacing out your expressions so that the < isn't next to other characters, so instead of:

 

for (var i=0;i<iMax;++i)

Try:

 

for ( var i = 0; i < Max; ++i) 

And see if that helps.

This was selected as the best answer
cloudgofercloudgofer

try using  contentType="text/plain" 

<apex:page showHeader="false" standardStylesheets="false" sidebar="false" contentType="text/plain" >

 

see all the content types supported by searching http://www.cloudgofer.com/?q=apex%20page%20contenttype

on the 1st result page , there is a link to content types. 

Andy Freeston_LarterAndy Freeston_Larter

Yep, that got it. It was the < followed by the variable name. So the fix would be:

 

for (var i=0;i< iMax;++i)

 

What's bizarre is the fact that I have many lines like this and only one exhibits the problem.

Anyway, thanks for the help. 

joshbirkjoshbirk

Yeah, it's a very very strange one.  I'll see if there are any flagpoles I can shake on it, but it's long standing thing with VF.

 

RARA

This is a known issue that was fixed in version 20.0. Which version is your page running on? We are not able to reproduce your issue in our environments.

 

joshbirk is right about the workaround: Placing a space character between the condition statement resolves the issue. E.g. 'i< 5' rather than 'i<5'

But you should not need to use this workaround if your page is using version 20.0.


Andy Freeston_LarterAndy Freeston_Larter

I can confirm that the page was set to version 19 (thedefault version for new pages created via Eclipse) and that increasing the version to 20 does fix the problem.

 

Cheers all.