I am having a scenario where I need to call a java method which takes in a string parameter which is Javascript code to be executed by the method.
executeJavaScript( String javascriptCodeToBeExecuted ) {
}
Right now, here is what I am doing
String javascriptCodeToBeExecuted = "function example(a,b) {" +
"number += a;" +
"alert(\'You have chosen: \' + b);" +
"}" ;
Since the amount of JS code is overwhelming, I would like to keep all javascript in external files and have my java program read the content as a string.
Is there a clean way of doing this ? Perhaps a library which takes care of escape sequences, etc ?
Thanks !
Yes, you can do this in many ways, e.g. plain Java:
or using Apache Commons:
Escape sequences are used just to escape the string in the Java code – if you read from file, no need for them.