0
onclick= "_deleteWPSchemeData(${viewWPMasterGrid.id}, '${viewWPMasterGrid.name}')"

${viewWPMasterGrid.name} retutrns me a string(for e.g. W.P.WINT OFF ALL'10) which often has single quote character so from the calling javascript method I am not getting the second parameter at all. How to deal with problem?

  • 1
    possible duplicate of [escape possible quotes in string passed to a js function in a onclick event](http://stackoverflow.com/questions/8593834/escape-possible-quotes-in-string-passed-to-a-js-function-in-a-onclick-event) – BalusC Jan 06 '12 at 12:28

4 Answers4

2

When a dynamic String can be put inside a JavaScript string literal, it should be JS-escaped. Just as when a dynamic String is put inside a HTML page, it's HTML-escaped.

Use commons-lang StringEscapeUtils.escapeECMAScript (or escapeJavaScript depending on the version) to escape the String. You could create a very simple EL function to do that straight from the JSP.

Note that you could have problems with single quotes, but also double quotes, tags, EOLs, backslash, which must all be escaped in a JS String literal.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

It looks like you could split the second parameter out into its own variable first. If I have understood your question correctly.

var viewWPMasterGridName = "${viewWPMasterGrid.name}";
onclick = "_deleteWPSchemeData(${viewWPMasterGrid.id},'" + viewWPMasterGridName + "')";
0

Use '${viewWPMasterGrid.name.replaceAll("'", "\'")}'

techfoobar
  • 65,616
  • 14
  • 114
  • 135
0

try this,

var name = "${viewWPMasterGrid.name}".replace(/'/g,"\\'");
dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58