I am trying to pass a Java String as a parameter for a JavaScript function. Yes I know Java and JavaScript are not the same, but I am using scriplets inside a .jsp.
I need a way to access the dynamically created java Scriplet object (componentTypePK) inside this JavaScript function. I thought the best way to do this would be to pass the Java Scriplet Object into the JavaScript function but I am getting the error described below.
This is my JavaScript Function:
<script LANGUAGE="JavaScript">
function showFormsInUse(componentTypePk){
window.open('/showFormsInUse.do?<%="documentTypePk="+request.getParameter("documentTypePk")+"programAreaPk="+request.getParameter("programAreaPk")+"ComponentTypePK="+componentTypePk');
}
</script>
Here is where I call the JavaScript function passing in a Java String as a parameter.
<% if(getOppList(context,componentType.getString("ComponentDef_pk")).length()>1){%>
<TD CLASS="ListCode">
<IMG onclick="showFormsInUse(<%=componentType.getString("ComponentDef_pk")%>)" SRC="images/check.gif" TITLE="<%=getOppList(context,componentType.getString("ComponentDef_pk"))%>" WIDTH="16" HEIGHT="15" BORDER="0">
</TD>
<% }else{%>
<TD CLASS="ListCode">
<IMG SRC="images/check.gif" TITLE="<%=getOppList(context,componentType.getString("ComponentDef_pk"))%>" WIDTH="16" HEIGHT="15" BORDER="0">
</TD>
<%}%>
I would prefer not to initialize a Java string before my script and then pass that String into a Javascript variable this. Also I know Java Scriplets are bad form, so I would love to know if I could use JSTL to fix the problem.
Update: When I try to run and compile this code I get the following:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 77 in the jsp file: /documentForms.jsp
Invalid character constant
74:
75: <script LANGUAGE="JavaScript">
76: function showFormsInUse(componentTypePk){
77: window.open('/showFormsInUse.do?<%="documentTypePk="+request.getParameter("documentTypePk")+"programAreaPk="+request.getParameter("programAreaPk")+"ComponentTypePK="+componentTypePk');
78: }
79: </script>
To summarize, is there any way to pass a Java Object into a JavaScript function?