Passing single and double quotes both in JavaScript function parameter from jstl (much tricky)

1.2k views Asked by At

I know this looks silly to ask, but I got a situation where I have to pass parameters to a javascript function, and these parameters are of String type.

The issue with data is, the data can have single Quote or Double quote

e.g.

"TEST", TEST or TEST. This is coming from the database.

here is my JSTL call on click.

The 2nd parameter value is ANOTHER " company" which is the actual root cause.

I tried JSTL fn: replace(.....) method as well but not succeed.

Note: The value also could be 'ANOTHER' Company.

<a href="javascript:void(0);" 
  onclick="return showPopUp('${account.fName}','${fn:replace(account.lName, "\'","\\\'")}','${fn:replace(account.compName, "\'", "\\\'")}','${account.firstTimePin}','${account.email}');return false;">
</a>

this generates the following HTML code.

<a href="javascript:void(0);" 
   target="_self" 
   onclick="return showPopup('CRUISE  ','ANOTHER " company"','123456','[email protected]');">
</a>

and here is my javascript function

function showPopup(param1,param2,param3,param4){
  // busness logic
}

The issue is invalid call/syntax to call showPopup() method.

1

There are 1 answers

11
brunnerh On BEST ANSWER

Encode it with &quot;.

function showPopup() { console.log(arguments); }
<a href="javascript:void(0);" target="_self" 
    onclick="return showPopup('CRUISE  ','ANOTHER &quot; company&quot;','123456','[email protected]');">
    Link
</a>

Just add another replace term, i.e.

fn:replace(fn:replace(account.compName, "\"", "&quot;"), "\'", "\\\'")