0

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.

4
  • there should be another quote at the start of national_id and also a box bracket is missing.. Commented Mar 17, 2014 at 7:03
  • 1
    Try setting the onload value with single quotes, not double quotes. The result of session.getAttribute() is using double quotes, so the nested quoting is messed up Commented Mar 17, 2014 at 7:05
  • @Santino'Sonny'Corleone actually whole national_id","birth_certificate"])"="" part should be in popuListbox fuction call as a parameter but its get misplaced after execution. thats the problem i face Commented Mar 17, 2014 at 7:06
  • @Sajirupee try this <body onload="popListbox(<%=session.getAttribute('objNames')%>)"> Commented Mar 17, 2014 at 7:13

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I guess your quotes are creating a problem.i.e they are conflicting..Try using single quotes...

<body onload='popListbox(<%=session.getAttribute("objNames")%>)'> 

3 Comments

Those quotes are fine as they are part of server-side code. The actual problem lies in the fact that his function returns JSON which uses double quotes.
@ThiefMaster v meet again(if u remember that is)...even i had a similar problem before but i solved with single quote as in my answer
Using single quotes for attribute names gives an error. but replacing double quotes in onLoad event resolved my issue. thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.