0
votes

I am having script errors of "Error expected ')'" in IE when attempting to do the following:

alert(<%=SystemConfig.getTranslatedTextByKey(LBIBOConstants.LANG_KEY_MODIFYTIME_ALRTMISSINGA, userLocale)%> + strMessageString + <%=SystemConfig.getTranslatedTextByKey(LBIBOConstants.LANG_KEY_MODIFYTIME_ALRTMISSINGB, userLocale)%>);

I would originally like to just alert with one string added together ahead of time, but I'ved tried that with no luck either.

My guess is you can't do something like this, but I don't have many other options (the code was developed by someone else, so I understand there are better alternatives than Scriptlets).

Regardless, these strings returned by JSP expressions are found in a constants.java file, which are in turn loaded from a language package, in case you all were wondering. I have double checked these, and they are just fine.

How can I add these strings together when one value is a var within the page, and the other values are within scriptlet tags? Is it possible?

1
Something interesting, is I stripped out the addition as a test, and just used ALRTMISSINGA's expression in the alert(), and that doesn't work either. Does anyone have the best approach in dealing with these scriptlets or accessing string constants from within my constants.java? - aohm1989
Wow, revisited this after noticing it became popular, and I'm going to tell you all something that I did in its place. This was for internationalization, in the end, and in which case you don't want to be appending strings that could be forced to be in a COMPLETELY different order for other languages. Don't ask me why I thought this was a good idea. I don't even use this code anymore, these were dark times, but it's fun to see how far I've come. - aohm1989

1 Answers

1
votes

JSP = server-side

JavaScript = client-side

The JavaScript code that the JSP generates must be valid JavaScript code. Look at the generated JavaScript using your browser "View page source" menu item, and fix the JSP code so that it generates valid JavaScript code.

Exactly how you would do if your JSP generated incorrect HTML code.

You're probably missing some quotes. But also make sure that the scriptlet expression don't return characters that should be JavaScript-escaped. Or use commons-lang StringEscapeUtils.escapeEcmaScript to escape those characters :

alert('<%= ... %>' + strMessageString + '<%= ... %>');
      ^----------^----------------------^----------^-- quotes!

And the expressions (...) should be your expressions escaped:

StringEscapeUtils.escapeEcmaScript(yourOriginalExpression)