I have a javascript code. Eg:
<script type="text/javascript">
AJS.$("#customfield_10306 option[value='-1']").remove();
(function($)
{
new AJS.MultiSelect(
{
element: $("#customfield_10306"), itemAttrDisplayed: "label", errorMessage: AJS.params.multiselectComponentsError
});
}
)(AJS.$);
</script>
I would like to put this in a java String type. So,
String someCode = above java script code
What is a quick and nice way of doing this rather than putting all the JS code in one line in quotes and using \ everywhere.
Thanks.
3 Answers 3
use stringbuffer then use append()
For example
StringBuffer buffer=new StringBuffer("<script type="text/javascript">");
buffer.append("AJS.$("#customfield_10306 option[value='-1']").remove();");
Comments
You can do apply some logic here
- Put all your JavaScript code in a file
- Read the file in Java code with the help of IO and put them to a string
Now your string is full of JavaScript written in file. If you will do so then your code will will be dynamic and you can change Javascript code any time. No java compilation will be required.
Simply use a multiline literal like this:
String s = "first line\n"
+ "second line";
As the strings are known at compile time the string will be concatenated then. No need to use a StringBuffer at runtime with constant strings.