0

I need to store the html retrieved from a <jsp:include> in a javascript variable. So I will have something like this

<script>
    var html = '<jsp:include page="...">';
</script>

The problem is the jsp file has lots of whitespace and newlines which makes the javascript invalid! I tried using the trimDirectiveWhitespaces directive as suggested here, but that does not remove newlines.

How can I remove newlines as well from html so it can be a valid javascript string?

Or, another solution is welcome as well.

EDIT:

The snippet should eventually look like this (but with many more options):

<script>
    var html = '<label class="someClass">Label</label><select><option value="val1">Value</option></select>';
</script>
Community
  • 1
  • 1
jlars62
  • 7,183
  • 7
  • 39
  • 60
  • what do you expect the snippet you posted to look like after trimming ? – jmj Jan 07 '15 at 01:22
  • this might help http://stackoverflow.com/questions/58054/how-can-i-replace-newline-characters-using-jsp-and-jstl – A M Jan 07 '15 at 01:28
  • 2
    Not sure how feasible it would be for you, but one way would be to make an AJAX call to run the JSP and get the HTML generated. – Bhesh Gurung Jan 07 '15 at 01:28
  • @BheshGurung The AJAX solution will work for now! Thank you. Still curious if there is an alternative/better way. – jlars62 Jan 07 '15 at 01:49
  • To avoid creating newlines, put everything in your included page on one line. – rickz Jan 07 '15 at 02:33
  • If your included page contains newlines you're out of luck, [see here](http://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript). So it seems Ajax is the way to go. – Antares42 Jan 07 '15 at 21:31

0 Answers0