0

I'm using JSF2 (Primefaces 2.x, and GlassFish 3.1.x).

I have a 'delete' button with a javascript confirmation.

<p:commandButton ajax="false" value="#{msg.common_delete}"
  action="#{profileHandler.doDelete}"
  onclick="return confirm('#{msg.profile_confirmDeleteMessage}')"
  immediate="true"
/>

The problem is the message in the resource bundle has an apostrophe. Like "Are you sure you want to delete the user's profile?"

The apostrophe is breaking the javascript so the confirmation message never appears, the button just does the delete.

I tried changing the text in the message bundle to ' and ' but neither worked; I just see the "'" in the message.

Any help is greatly appreciated!

Rob

2 Answers2

0

You can try to add \' in the message properties file befor the ' character.

mateusz.fiolka
  • 3,032
  • 2
  • 23
  • 24
  • Then the easiest, although not the most elegant solution for sure would be to render the value to a hidden element on a page. Then you could use document.getElementById or $("#id") using jQuery in the confirm function. – mateusz.fiolka Sep 13 '11 at 20:29
  • 2
    You have to escape it a lot of times since Java and JavaScript both use backslash to escape. Try: `\\\'`. The first \\ is to add a single \ to the Java string. The \' is to add the ' to the Java string. The result should be sent to the browser as \' which is an escaped single quote in JavaScript. – Hemlock Sep 14 '11 at 03:09
  • @Hemlock You should post your comment as an answer because it is the correct answer. – Sébastien Gicquel Jan 02 '14 at 15:36
0

Does JSF2 allow single quoted attributes? If so, you can try switching out the outer qutoes to be single quotes and the inner ones to be double quotes. That way js shouldn't get confused.

onclick='return confirm("#{msg.profile_confirmDeleteMessage}")'
mschor
  • 141
  • 4
  • ok. alternatively take a look at this: [http://stackoverflow.com/questions/73628/how-can-i-execute-javascript-before-a-jsf-hcommandlink-action-is-performed](http://stackoverflow.com/questions/73628/how-can-i-execute-javascript-before-a-jsf-hcommandlink-action-is-performed) and also try with immediate not set to true. That might be blowing away your onclick. Used JSF1.2 a long time ago. – mschor Sep 13 '11 at 21:13