Adding javascript for loop into document.bplt causes page error

book

Article ID: 111488

calendar_today

Updated On:

Products

CA API Developer Portal CA API Gateway

Issue/Introduction


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> 
 

Cause

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

Environment

API Portal 3.5 


 

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.