Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Escaping characters

    Hi,

    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

    Thanks
    Ricki Stern

  • #2
    New Coder
    Join Date
    Jul 2002
    Location
    Portugal
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    use the escape function to 'encript' the string

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

    and the unescape for the oposite

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

  • #3
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried that, but the problem is that when i pass a string with a ' into the escape function that also fails

  • #4
    New Coder
    Join Date
    Jul 2002
    Location
    Portugal
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)+'\')"')

  • #5
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    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>

  • #7
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks!

    the \' worked like a charm


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •