This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes Passing String from struts to javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Passing String from struts to javascript" Watch "Passing String from struts to javascript" New topic
Author

Passing String from struts to javascript

Tankred Smult
Greenhorn

Joined: Jul 15, 2003
Posts: 16
Hi all!

I'm trying to pass a string from my jsp page to javascript, to make a javascript popup with a printerfriendly edition of my page. But I'm having a problem when the string contains a newline('\n'). It totally messes up my javascript:

win1.document.write('<nested:write property="answer"/></td>');

As I said, this works like a dream when there's no \n in "answer". I was hoping there was a way to strip out the \n's from the property. I've tried the filter option, hoping that it would strip out the \n, or even maybe replace them with <br/>s.

Do anyone know how to remove the \n's?

.tankred
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
How about doing that in your getAnswer() method?
Tankred Smult
Greenhorn

Joined: Jul 15, 2003
Posts: 16
Well, not really. You see, I use the same formbean to extract the data, and in other cases I would like the \n to be included. It is just in the special case where I use it to generate a javascript string that I would like to replace it. So, if there were some kind of filer I could create and use, that would replace the \n's, that would be the best solution. Is that possible?

.tankred
Tankred Smult
Greenhorn

Joined: Jul 15, 2003
Posts: 16
Hi.

Just wanted you to know I hacked my way around it.
I put the field in html as a hidden field:

and then get the value in javascript by reading that property:

It's what we in norway would call a "grisehack" (pighack), but it seems to work.

Still, if anyone know a more elegant solution, please let me know.
 
Don't get me started about those stupid light bulbs.
 
subject: Passing String from struts to javascript
 
Similar Threads
Hibernate + Spring... mapping files not loading
using ajax and struts iterate tags
Help with javascript
problems with Session.save()
Struts - Validation