Home
Analytics
How to force JavaScript code
alcorus
<p>Hello,</p>
<p> </p>
<p>I recently upgraded my 2.2 reports to Birt 4.5.</p>
<p>In one of them this JS function is called:</p>
<pre class="_prettyXprint">
String.fromCharCode(charCode)</pre>
<p>Since the upgrade, it's not working anymore.</p>
<p>Here's the error: (translated from french)</p>
<p> </p>
<div><span style="color:#ff0000;">ReportDesign (id = 1):</span></div>
<div><span style="color:#ff0000;"><span>+ </span>There are errors evaluating script "String.fromCharCode(charCode)":</span></div>
<div><span style="color:#ff0000;">Java class "java.lang.String" doesn't have any field nor any public instance method called "fromCharCode"</span></div>
<div> </div>
<div>Is there a way to force the use of JavaScript instead of Java? or an alternate code to do it?</div>
<div> </div>
<div>Many thanks,</div>
<div>David</div>
Find more posts tagged with
Comments
Clement Wong
<p>Where is it not working for you?</p>
<p> </p>
<p>Are you able to send a sample design that replicates the issue?</p>
<p> </p>
<p>As a test, I was to use String.fromCharCode(65, 66, 67) in the <em>beforeFactory </em>event, and in a HTML Text item. The attached report design was tested in both iHub 3.1.1 and Open Source BIRT 4.5.0</p>
alcorus
<p>Hello Clement,<br>
first thank you for your quick reply.<br>
Actually I forgot to say i'm using a javascript include file.<br>
here you can find a test report along with my js file (please rename by removing the .txt)<br>
Best regards,<br>
David</p>
alcorus
<p>It seems that when you put the java.lang import somewhere in the report or inside a js file,</p>
<p>the java String class takes priority over the javascript one.</p>
<p>So how to force access to the javascript one?</p>
Clement Wong
<p>In the first line of the reporting.js file:</p>
<p> importPackage(Packages.java.lang);</p>
<p> </p>
<p>that imported the package which includes java.lang.String.</p>
<p> </p>
<p>So here are two alternatives and I'm sure there are more...</p>
<p> </p>
<p> </p>
<p>1. Use a different method to construct the single character</p>
<pre class="_prettyXprint">
function fromCharCode(charCode) {
return (new Character(charCode).toString());
}
</pre>
<p>2. Declare a variable to save the Rhino JavasScript String before the importPackage, and use that saved variable in fromCharCode</p>
<pre class="_prettyXprint _lang-">
var JavaScriptString = String;
importPackage(Packages.java.lang);
...
function fromCharCode(charCode) {
return JavaScriptString.fromCharCode(charCode);
}
</pre>
<p>
</p>
alcorus
<p>The javascript variable is indeed a great way to allow using both js and java after the import.</p>
<p>Thank you for you great answer Clement :-)</p>
<p> </p>