最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How can I display special chars in an alert box using javascript? - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 6

Your 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

发布评论

评论列表(0)

  1. 暂无评论