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
mejoemejoe 

javascript in vforce page causing validation errors when saving

I need to implement a visualforce page that serves a javascript text file.  This is based on this blog entry: http://www.nimbleuser.com/blogs/Developers/Developers/Visualforce_Static_Resource_Alternative.aspx

 

<apex:page contentType="text/javascript" cache="false" expires="0">
//alert('ping');
if (document.getElementById('cas21') != null)
 {
    var divsInthepage = document.getElementsByTagName("DIV");
    for (i = 40; i < divsInthepage.length; i++)
    {

        if (divsInthepage[i].innerHTML.indexOf("topButtonRow", 1) > 1 && divsInthepage[i].innerHTML.indexOf("requiredBlock", 0) < 1)
        {
            divsInthepage[i].innerHTML = '&lt;table  border="0" cellpadding="0" cellspacing="0"&gt;&lt;tr&gt;&lt;td class="pbTitle"&gt;&lt;img src="/s.gif" alt="" width="1" height="1" class="minWidth" title="" /&gt;&lt;h2 class="mainTitle"&gt;Case Edit&lt;/h2&gt;&lt;/td&gt;&lt;td class="pbButton" id="topButtonRow"&gt;&lt;input value="Submit"  class="btn" title="Submit" name="save" type="submit" /&gt; &lt;input value="Cancel"  class="btn" title="Cancel" name="cancel" type="submit" /&gt;&lt;/td&gt;'
        }
        if (divsInthepage[i].innerHTML.indexOf("bottomButtonRow", 1) > 1 && divsInthepage[i].innerHTML.indexOf("requiredBlock", 0) < 1)
        {
            divsInthepage[i].innerHTML = '&lt;table  border="0" cellpadding="0" cellspacing="0"&gt;&lt;tr&gt;&lt;td class="pbTitle"&gt;&lt;img src="/s.gif" alt="" width="1" height="1" class="minWidth" title="" /&gt;&lt;/td&gt;&lt;td class="pbButtonb" id="bottomButtonRow"&gt;&lt;input value="Submit"  class="btn" name="save" tabindex="11" title="Submit" type="submit" /&gt; &lt;input value="Cancel"  class="btn" name="cancel" title="Cancel" type="submit" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;'
        }
        if (divsInthepage[i].innerHTML.indexOf("Assign using active assignment rules", 1) > 1 && divsInthepage[i].innerHTML.indexOf("requiredBlock", 0) < 1)
        {
            divsInthepage[i].innerHTML = '&lt;table  class="detailList" border="0" cellpadding="0" cellspacing="0"&gt;&lt;tr  class="detailRow last"&gt;&lt;td colspan="4"&gt;&lt;input  checked="checked" id="cas21" name="cas21" tabindex="10" type="checkbox" value="1" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;'
        }
        if (divsInthepage[i].innerHTML.indexOf("&lt;h3&gt;Optional&lt;span", 1) > 1 && divsInthepage[i].innerHTML.indexOf("requiredBlock", 0) < 1)
        {
            divsInthepage[i].innerHTML = ''
        } {
    }
}
</apex:page>

It seems that any occurence of "<" or "&" is triggering the issue. I discovered that setting the version number for the file to 18 fixed the issue.  What can I do to format the javascript so that it does not cause validation errors?

TheSwamiTheSwami

What error are you getting?

mejoemejoe

"The content of elements must consist of well-formed character data or markup." as well as an error about "&" needing an entity after it.

cwall_sfdccwall_sfdc

Prior to v18, Visualforce did not enforce XHTML formatting.  After v18, all Visualforce markup must be valid XHTML.  Javascript in a Visualforce page is not valid w/o a <script> tag.

 

What you're missing is that the blog's example defines Javascript functions.  If you want your Javascript executed during page loading, wrap your Javascript in a function and call it immediately.  For example:

 

 

<apex:page contentType="text/javascript" cache="false" expires="0">
// executed during page loading
(function() {
  alert('ping2');
})();
 
// regular JS function
function ping() {
    alert('ping');
};
</apex:page>

 


richardc020richardc020
Is it thus bad practice to serve js, even if it's very frequently edited, as an apex page?

i.e.
<apex:page contentType="text/javascript" showChat="false" showHeader="false" sidebar="false" readOnly="true">
//js here
</apex>

due to all the xhtml rendering errors that'll occur with any characters like "&&", etc?