I’m setting the following in the request attribute:
confirmMsg = "REf No: \n 112";
req.setAttribute("confirmMsg", confirmMsg);
I need the above to be displayed like(in alert box) :
Ref No:
112
I’m using the following onload function in my JavaScript to show the message on loading the page.
function onLoad() {
var msg = "${confirmMsg}";
if(msg != null && msg != "") {
alert(msg);
}
}
The above throws a script error? How do I pass the newline character?
The
\nneeds to be JavaScript-escaped to be put in a JavaScript string literal.Use commons-lang
StringEscapeUtils.escapeECMAScript()to eascape the message, and pass this escaped message to your JSP.