tags:

views:

21

answers:

2

i have in my jsp a js function to open a popup, in this function I pass as parameter, this parameter is a message like this

L'appareil est en panne

but when i load my page i have a javascript. how i can i do before i load my page to do this*

L''appareil est en panne

A: 

You can escape quotes inside strings using backslashes:

alert('L\'appareil est en panne\'');

there's a Java class that seems to do the job: StringEscapeUtils.escapeJavaScript(). Boy, you Java guys have a class for everything!

Pekka
ok, but how can i do this in my java class ..?
Mercer
@Mercer I don't know much Java but I'm sure there's a built-in method to escape a string.
Pekka
@Mercer is this any good? http://www.java2s.com/Code/Java/Apache-Common/StringEscapeUtils.htm
Pekka
Javascript is not the same as Java. Either way, trySystem.out.println("L\'appareil est en panne");
Nubsis
@Nubsis I think the OP wants to output a string from Java that is going to be interpreted in Javascript.
Pekka
thx it's workStringEscapeUtils.escapeJavaScript(unescapedJavaScript));
Mercer
A: 

Altough your question is not very clear. But you can use escape character \' to resolve such problems.

sushil bharwani
sorry for my bad english, but i create my message in a java class so i want to replace my ' with escape but how can i do
Mercer
can you paste in some code. I believe i can help you with it.
sushil bharwani
String str = "L'appareil est en panne";str.replaceAll("'","\');System;out.println(str); in the console:L'appareil est en pannedon't work :(
Mercer
i use the Pekka solutionStringEscapeUtils.escapeJavaScript(unescapedJavaScript));it's work for me thx for yur response
Mercer