Archive | javascript
Mixing inline JSP code with JavaScript
Before I get started, let us all repeat three times: “Inline JSP is evil.” Now that we are all on the same page . . .
I’ve inherited a project that has a ton of JSP’s with inline code. This code specifies a lot of logic and decisions that, frankly, should be back at the Action. If that wasn’t bad enough, a lot of the JSP code is embedded in JavaScript functions. Huh? JSP helping generate JavaScript? Yessir. And it’s worse than you think.
In my ideal world, this would be the only way that JSP code would integrate into JavaScript:
foo(< %=varValue%>);
Then the function foo would with whatever the value of varValue would be.But, hey, that’s ideal. I could live with the following (think of this in the middle of a JavaScript function):
var newVar = < %=varValue%>;
That’s just copying the value of a Java object to a JavaScript object. Not great, but livable.The following isn’t as good as the above, but I can deal with it:
var newList = < %=varValue%>.split(",");
A cheap and dirty way to to make an array, huh? However, the next one should never be allowed:
< % for (int i=0; i<=sArray.length; i++) {
if (sArray[i]!=null) { %>
myArray[< %=i%>]=< %=sArray[i]%>;
< % } %>
< % } %>
I’m trying to figure out why anyone would think that mixing Java loops and if-statements in the middle of JavaScript function is a good idea. It’s not readable, it’s not changeable, and it will break when the next person edits that function. C’mon. Really.
This is why JavaScript is becoming my 2007 Language of the Year — because things like the above need to die.
