0

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.

asked Apr 22, 2014 at 11:37

3 Answers 3

1

use stringbuffer then use append()

For example

 StringBuffer buffer=new StringBuffer("<script type="text/javascript">");
buffer.append("AJS.$("#customfield_10306 option[value='-1']").remove();");
answered Apr 22, 2014 at 11:38
Sign up to request clarification or add additional context in comments.

Comments

0

You can do apply some logic here

  1. Put all your JavaScript code in a file
  2. 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.

answered Apr 22, 2014 at 11:52

2 Comments

Thanks. This looks like an interesting approach!
accepted because your answers satisfies the following criteria for my question: "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."
0

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.

answered Apr 22, 2014 at 11:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.