Meaningless Drivel is fun!
The moose likes Servlets and the fly likes ESCAPE tic marks (') in javascript and jsp??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "ESCAPE tic marks ( Watch "ESCAPE tic marks ( New topic

ESCAPE tic marks (') in javascript and jsp???

verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
I have a link in my jsp page that calls a javascript function. The values I pass into the function are determined dynamically. My question is how do I escape tic marks (') when calling the function? My code is:
<A href="javascript: diffVisit('<%= visit.getAccountNumber() %>','<%= visit.getRoomBed() %>','<%= visit.getAdmitDateFormatted() %>','<%= visit.getDischargeDateFormatted() %>','<%= visit.getAdmittingDiagnosis() %>','<%= i %>', '<%= visit.getAttendingDoctor().getNameLastNameFirst() %>', '<%= visit.getHeight() %>', '<%= visit.getWeight() %>', '<%= visit.getReferringDoctor().getNameLastNameFirst() %>', '<%= visit.getLengthOfStay() %>', '<%= visit.getConsultingDoctor().getNameLastNameFirst() %>', '<%= visit.getDischargeDisposition() %>');"><%= visit.getAdmitDateFormatted() %></A>
When the values are filled in, some of the names could be O'Mally or O'Brian. With the tics in the name, this causes the javascript not to execute properly. For example, the above becomes:
<A href="javascript: diffVisit('0000524357','652157','12/09/1999','','UNSTABLE','0', 'PULLMANN MD, DANIEL E', '6 ft. 2 in.', '172 lbs.', 'O'DOWD MD, MARY K', '0', 'O'MALLY MD, JERRY H', 'Discharged to home');">12/09/1999</A>
Any ideas on how to solve the above situation is greatly appreciated!!
Kelley Koszegi

Joined: Aug 20, 2001
Posts: 27
I believe a '\' can be used as an escape character.

Kelley Koszegi<br />Sun Certified Java Programmer
verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
Thanks for your response, Kelley. I have tried using '\' as an escape character, and that seems to work. Now I am writing a utility method to check for tics and escape them before sending them to the javascript function. However, I'm missing something in my function:

For example, when I send in "6' 2''", the method returns "6\' 2\''" instead of "6\' 2\'\'". I've tried using indexOf("'") and indexOf('\''), but neither seem capable of capturing the last ' and escaping it correctly. I know this is a small mistake, but I can't see what I'm doing wrong. Your corrections are appreciated!
[This message has been edited by verduka fox (edited August 22, 2001).]
I agree. Here's the link:
subject: ESCAPE tic marks (') in javascript and jsp???
It's not a secret anymore!