I'm trying to pass a HTTP Session
attribute from a JSP page to a JavaScript function written in external JS file. When I'm writing JS part in body section without writing specific function it works fine but when I declare a function and try to pass session attribute as a parameter it gives following error.
SyntaxError: syntax error
I have tried following methods to pass session attribute to JS function:
<body onload="popListbox(<%=session.getAttribute("objNames")%>)">
<body onload="popListbox(${objNames})">
after execution body tag looks like this:
<body national_id","birth_certificate"])"="" onload="popListbox(["></body>
Can someone tell me why body tag looks like above and how should I pass the parameter to JS function.
You could use single quotes for the attribute, i.e. onload='...'
- right now your data string contains double quotes which break everything as they terminate the attribute.
However, it would be much better to keep using double quotes and HTML-escape any special characters ("
, &
, <
and >
) in objNames
! That way you don't get any problems if the data contains single quotes somewhere.