View Full Version : Escaping characters

07-23-2002, 02:40 PM

I need to have a link on a page that contains a few javascript commands (i.e. <A href="javascript: ...... ) Anyway one of the commands sets the value of a textbox to a string (document.myform.text1.value = 'Text';)

The problem is that sometimes Text contains a ' char. So my Q is how in javascript do I escape that char

Ricki Stern

07-23-2002, 02:46 PM
use the escape function to 'encript' the string

escape('my name') -> my%20name

and the unescape for the oposite

unescape('my%20name') -> my name

07-23-2002, 02:50 PM
I tried that, but the problem is that when i pass a string with a ' into the escape function that also fails

07-23-2002, 03:18 PM
how are you placing that string.. with ASP..

if so then

<a href="javascript:someobj.innerHTML = unescape('<%=escape(someASPstring)%>')">

if it's all DHTML then maybe it should look something like this..

document.write('<a href="javascript:someobj.innerHTML = unescape(\''+escape(someDHTMLstring)+'\')"')

07-23-2002, 05:27 PM
Im not sure what you mean. The code is exactly what I wrote origionally. I have a bunch of names hyperlinked

<a href="document.myform.text1.value='O'mally';">O'mally</A>
<a href="document.myform.text1.value='Smith';">Smith</A>
<a href="document.myform.text1.value='Bush';">Bush</A>

07-23-2002, 05:47 PM
That kind of character escaping/unescaping is for URL-forbidden characters, nothing to do with JavaScript strings.

<a href="document.myform.text1.value='O\'mally';">O'mally</A>

07-23-2002, 05:50 PM

the \' worked like a charm

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum