Skip to content Skip to sidebar Skip to footer

Escape Possible Quotes In String Passed To A Js Function In A Onclick Event

I have the following cycle in a jspx:
  • Solution 1:

    Solution 2:

    You don't need to create your own EL function but use apache-commons directly from your custom .tld:

    <function>
        <name>escapeJavaScript</name>
        <function-class>org.apache.commons.lang.StringEscapeUtils</function-class>
        <function-signature>java.lang.String escapeJavaScript(java.lang.String)</function-signature>
    </function>
    

    Solution 3:

    I suggest you encode on the server http://www.roseindia.net/jsp/jsp-url-encoding.shtml

    or store the stuff in a hidden element

    <span id="url" style="display:none">${var.URL}</span>
    <span id="title" style="display:none">${var.title}</span>
    <span id="desc" style="display:none">${var.descr}</span>
    

    and do onClick="return myFunct(['url','title','desc'])">...</a>

    functionmyFunct(parms) {
      var url   = parms[0]?document.getElementById(parms[0]).innerHTML:"No url";
      var title = parms[1]?document.getElementById(parms[1]).innerHTML:"No title";
      var descr = parms[2]?document.getElementById(parms[2]).innerHTML:"No description";
      returnfalse;
    }
    

    Solution 4:

    You need to pass a valid JavaScript String literal to myFunct. escape is a JavaScriptfunction that expects a valid String as well. You thus need to transform you Java String into a valid JavaScript literal. Use apache commons-lang StringEscapeUtils.escapeECMAScript to escape it. You could make it an EL function, and thus use something like

    onClick="myFunct('${myFn:escapeJs(var.url)}','${myFn:escapeJs(var.title)}, '${myFn:escapeJs(var.descr)}');"
  • Post a Comment for "Escape Possible Quotes In String Passed To A Js Function In A Onclick Event"