0

My code in JSP file looks like this :

<s:form namespace="/user" action="list" method="POST" id="filterListForm" theme="simple"
        onsubmit="document.getElementById('filterSearchText').value=document.getElementById('filterSearchText').value.replace(/\\/g,'')">

It won't replace the backslash char. I've tried the following, none of them work :

replace('/\\/g','')
replace(/\\\\/g,'')
replace(\/\\\/g,'')

But if I change it to the following, it works :

<s:form namespace="/user" action="list" method="POST" id="filterListForm" theme="simple"
        onsubmit="replaceBackslash()">

<script type="text/javascript">
  function replaceBackslash() { document.getElementById('filterSearchText').value=document.getElementById('filterSearchText').value.replace(/\\/g,''); }
</script>

Why ? Is there a way to make it work in the first case ?

1
  • JSP itself has to parse string constants, and putting JavaScript inside of such strings is going to cause trouble. Commented Oct 3, 2012 at 20:20

2 Answers 2

4

You want:

var replaced = original.replace(/\\/g, '');

In a regular expression literal, all you need to do is double the backslash to quote it.

As to why it doesn't work when you try passing the code in via a JSP tag, well that would probably be JSP mangling the string for you. It might work to do this:

<s:form ... onsubmit=' ... .replace(/\\\\/g, "") ... ' >

but I don't have a good way to try that at the moment.

edit — actually I'm finding this challenging. It probably depends on what your tag library does. My framework (Stripes) likes to HTML-escape attribute values, so it's hard to pass through something like &#92; (well, impossible).

Sign up to request clarification or add additional context in comments.

5 Comments

I'm missing how this is different from what he already tried.
@Kiyura it's not - he says that what he already tried works. The problem with his first sample is that the string he's passing to the JSP tag is being mangled by the JSP translator, I think.
Yes, but the question seems to be how to make it work in the first case - inline in the JSP. He already seems to know that it works outside (even if he might not have known precisely why).
Arguably it's best to just not have inline Javascript in JSP. I know I've run into quite a bit of trouble with that myself.
Yes you're right - I updated my question; I can actually try this out as I'm working on a large JSP project myself :-)
0

(This isn't really a solution, just a recommendation of a general practice that happens to solve this problem, too.)

Bottom line: Go with separated Javascript. If you feel it's too much work to completely separate it out into a different file (even though that would help you cleanly avoid all issues such as this), at least put it all in a script tag at the bottom. It helps separate layout and logic, and it keeps all the Javascript in one known place, making it easier to understand and maintain. You don't even need onclick/onsubmit attributes, you can assign those in Javascript too (usually keyed on html #id attributes). If you use the on[event] attributes anyway, just call one sensibly named function, and put the function's implementation in your main script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.