0

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"].

Chuck L
  • 1,026
  • 10
  • 23
  • 1
    Can you clarify **exactly** what that method returns? Don't use quotation marks when showing the value, show the actual characters that are in the return value and nothing else. – Pointy Mar 14 '14 at 00:00
  • View the page source in browser and copy and paste the offending line here – andrew Mar 14 '14 at 00:05
  • I've added the line from the source in the browser. I think I may see the issue, the double quotes around Print may need to be escaped? – Chuck L Mar 17 '14 at 15:29

2 Answers2

0

It looks like you need to add the quotes around the string value. Your asp.net code will return a string, but you need to have quotes around it to be treated as a string in javascript:

<body onLoad="initPage('<%=object.getStringWithBrackets()%>'); otherfunction();">
ps2goat
  • 8,067
  • 1
  • 35
  • 68
  • I think that the function expects an Array, or what he's doing wouldn't make much sense (at least to me) – Oscar Paz Mar 14 '14 at 00:16
  • yeah, either way more info is needed to confirm since we all have different assumptions! But I can certainly see your point. – ps2goat Mar 14 '14 at 00:30
  • @ps2goat I did have single quotes to begin with and it didn't seem to work. There's no use of asp.net. Just javascript, html and java. – Chuck L Mar 17 '14 at 15:30
  • ok. Where is the `<%= %>` syntax coming from, then? I'm familiar with that in asp.net webforms code. – ps2goat Mar 18 '14 at 14:51
  • You can check my answer to this post: http://stackoverflow.com/questions/22298591/syntax-of-javascript-contains/22485788#22485788 – Chuck L Mar 18 '14 at 16:49
0

It looks to me like your problem is that you're returning literally "['Name']" and you interpolate into an onLoad value that is itself wrapped in single quotes. I can't trace exactly why you'd end up with precisely what you're saying you're getting, but I'm guessing you didn't supply us your exact javascript code and that the onLoad is wrapped in the same quotation as the Name ( here you show single quotes where the string is being terminated ). So you end up writing out html that looks like this:

The onload becomes "initPage([" ( from your error I'm seeing the onLoad setting is terminated after the [ so it must have the same quotation as the onLoad string itself) and that of course, is not valid javascript.

You can of course pass strings with parameters as javascript. You can also pass arrays as parameters. But you cannot write both ' and " into a javascript expression which is itself enquoted with either one, unless you escape it.

Hope it helps!

erik258
  • 14,701
  • 2
  • 25
  • 31
  • I think you may be right. I just updated my question. Let me try to escape and see if that helps. Would you suggest a better way of going about this other than to escape the extra quotes? This will eventually become a list of different strings in a list. – Chuck L Mar 17 '14 at 15:35
  • Hi, I'm definitely right, looking at your well made edit above. From the exerpt `onLoad="initPage('["Print"]');` you can see that the onLoad value ends at the `"` just before `Print` - causing both a syntax error and a truncated javascript. My preferred method would be to set the variable in a piece of un-enquoted javascript ( assuming it's being written out on the server side, set it as `` and then use `onLoad="initPage(initPageValue)..."`- Inline JS should be minimized. You might also consider whether you could use better format than `'["Print"]'` – erik258 Mar 18 '14 at 14:51
  • Hah! That's what I exactly did. I ended up saving the value of the java method into a variable and then referencing that variable when calling the javascript function. Thanks again for the help Dan! – Chuck L Mar 18 '14 at 16:26