1

I need to send variables at runtime to a js method for ajax. I am trying with this onclick function but it gives syntax error on click.:

<c:set var="abc" value="${myTaglib:getAbc()}" />
<a href="#" onclick="sendAjax(${abc.firstVal}, ${abc.secVal}, ${abc.thirdVal}, ${abc.fourthVal}, ${abc.fifthVal})"> Click Here !!!</a>

Some of these values may have spaces and decimals in them. On click, I get this error at the first occurrence of space.:

SyntaxError: missing ) after argument list

The actual HTML looks like:

<a onclick="sendAjax(240265, Workplace Ethics Exam, Company Security Policy, Friends Life, ABF/45RFG, 41444.1830)" href="#">Click Here !</a>

What is wrong with this JS call ?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Riju Mahna
  • 6,718
  • 12
  • 52
  • 91
  • What is the *actual* HTML returned? That will indicate exactly where the error(s) are - in this case that is trying to write invalid JS literals; probably text values that are not correctly written as string literals. The `${..}` interpolation itself *does not* understand the "JavaScript code" context or how to correctly encode the value. – user2864740 Feb 23 '15 at 03:38
  • Now, *assuming* that the values never contain quotes (either single or double), then `sendAjax('${abc.firstVal}', '${abc.secVal}', ..)` would "fix" that result. Such is still a terrible hack though (as it is manual and will "break randomly" if there are quotes and in some other edge cases); there should be a suitable encoding function - I've created such in C# and I'm sure there is similar for Java/JSP .. – user2864740 Feb 23 '15 at 03:43
  • A related / duplicate question http://stackoverflow.com/questions/27496052/jsp-variable-as-javasript-function-parameters?rq=1 (it is not exact because of a different context though!) - with a barely passable / adaptable solution. Ideally there would be a "somewhat elegant" way to do this. – user2864740 Feb 23 '15 at 03:48
  • Note to answers: can someone *please* post a reliable solution, taking the above into account, that will result in guaranteed valid JavaScript for any variable value? Out of several [near] duplicates, all are "manual quote" hacks. – user2864740 Feb 23 '15 at 03:51
  • Well, I can't find much on the subject - google and all. Given my limited "known solution" and my dislike for the hack, I'd write a custom function (http://stackoverflow.com/a/6396886/2864740) and then use that as an appropriate wrapper. It would look like: `sendAjax(${js:lit(abc.firstVal)}, ${js:lit(abc.secVal)}, ..`. Then it's just a matter of writing the "lit" or (to "make a safe literal") function to return a correct value which should be relatively easy.. following the convention that the literal will use single quotes for strings and escape double quotes (used for the attribute quotes). – user2864740 Feb 23 '15 at 04:04

2 Answers2

1

Spaces in your JavaScript code are most likely a syntax error. If your values have spaces, it's best to treat them as strings.

onclick="sendAjax('${abc.firstVal}', '${abc.secVal}', ... '${abc.fifthVal}')">

If you want to use them as numbers later, you can use the JavaScript functions parseInt and parseFloat to turn strings into numbers.

Hylianpuffball
  • 1,553
  • 10
  • 13
0

Did you try leaving a space after the last curly brace?

amahfouz
  • 2,328
  • 2
  • 16
  • 21
  • yes I did.. no effect. I am not sure if I need to use quotes and escape them... But then any of these variables could be string or an integer of a Double with decimal. – Riju Mahna Feb 23 '15 at 03:38