Adding javascript for loop into document.bplt causes page error

Document ID : KB000111488
Last Modified Date : 17/08/2018
Show Technical Document Details
Issue:

The code that failed (minimal set as we resolved down to the not working stmt) added to the  document.bplt : 
<script>
var i;
for (i = 0; i < dropdown.length; i++) {

}
<script> 
 
Environment:
API Portal 3.5 


 
Cause:
The javascript code needs to be in xml safe format, the "<" is clearly the problem.
 
for (i = 0; i < dropdown.length; i++) {
Resolution:

When adding the javascript code to the document.bplt file - the following two solutions will work:  

1) escape all as CDATA section
 <script>
      <![CDATA[
        var i;
        for (i = 0; i < dropdown.length; i++) {

        }
      ]]>
</script>

2) encode any xml special character :  
 
 <script>
        var i;
        for (i = 0; i &lt; dropdown.length; i++) {

        }
</script>
 
Additional Information:
Initially we had isolated that it was the "for" loop that was failing.    The first workaround we coded used a recursive call to do the looping -  that did work.

But after feedback from Engineering it made sense what the problem was - with hindsight the answer is obvious.