I am calling a javascript function in the onLoad of the body element in one of my webpages. Looks like this:
<body onLoad="initPage(<%=object.getStringWithBrackets()%>); otherfunction();">
getStringWithBrackets()
is a java function and will return a string like this: "['Name']"
When this happens a syntax error occurs: SyntaxError: syntax error initPage([
And there's a little arrow that points to after the bracket.
If I surround the scriplet with single quotes, the little arrow points inbetween the single quotes and bracket: initPage('[.
Are we not allowed to pass strings with brackets as parameters?
Edit: Thanks for the help so far. This is the line of html that is failing (I copied from the source after running into the error. I just changed names for the other function, everything else is untouched), I've put the single quotes back in around the scriplet to show the failure:
<body onLoad="initPage('["Print"]'); otherfunction('param1', 'param2', 'param3', 'param4', 'param5');">
Also wanted to mention that the java method getStringWithBrackets()
is using Gson to return a json string version of java List(). That's how I get ["Print"].