I want to display special chars as an alert box using javascript and jsp...
String encodeString = "ss\ncc";
String test = "DisplayNext('"+encodeString+"')";
String NextLink = "<br><a href='#' onclick="+test+"> Next</a>";
That is
function DisplayNext(Next){
alert(Next);
}
Though I've used special chars I am not able to display them in an alert box. How can I sort this out?
I want to display special chars as an alert box using javascript and jsp...
String encodeString = "ss\ncc";
String test = "DisplayNext('"+encodeString+"')";
String NextLink = "<br><a href='#' onclick="+test+"> Next</a>";
That is
function DisplayNext(Next){
alert(Next);
}
Though I've used special chars I am not able to display them in an alert box. How can I sort this out?
Share Improve this question edited May 17, 2017 at 11:02 cweiske 31.2k15 gold badges147 silver badges205 bronze badges asked May 12, 2011 at 8:46 Manish BasdeoManish Basdeo 6,27923 gold badges72 silver badges103 bronze badges 1- 8 You've posted 67 previous questions. With respect, you should be able to format code by now. See the handy How to Format box to the right of the question area, and the page linked from the [?] just above the question area. – T.J. Crowder Commented May 12, 2011 at 8:48
2 Answers
Reset to default 6Your code produce something like this:
<br><a href='#' onclick="DisplayNext('ss
cc');"> Next</a>
And what you need is:
<br><a href='#' onclick="DisplayNext('ss\ncc');"> Next</a>
If you want a line break in javascript it must look as \\n
in java. So use:
String encodeString = "ss\\ncc";
String test = "DisplayNext('"+encodeString+"')";
String NextLink = "<br><a href='#' onclick="+test+"> Next</a>";
Also consider using a special function to escape your String objects as javascript values. Google will easily help you find it ;)
If your String is URLEncoded in java you need to unescape it in javascript.
Java:
String s = "ë";
System.out.println(URLEncoder.encode(s, "ISO-8859-1"));
this will print out %EB
Javascript:
alert(unescape('%EB'));
this will print out the character ë in alert message