Single and double quote problems.

Discussion in 'Javascript' started by Diarmaid McGleenan, Nov 10, 2004.

  1. Hi all. This isn't quite the same single/double-quote problem we see
    posted a multitude of times in this ng. Read on...

    I have a js function called ShowActiveLink which accepts a string
    containing the full "<a href...>" element. My problem is that some of
    the hrefs contain both single and double quotes and I don't have the
    ability to escape these characters manually because our content
    management system inserts the href when it generates the site.
    Therefore I end up with code like this:

    <script language="javascript">
    void ShowActiveLink('<a
    href="javascript:newWindow('http://www.vre.org/');">VRE Service
    Status</a>');
    </script>
    <a href="javascript:newWindow('http://www.vre.org/');">VRE Service
    Status</a></li>

    ....which causes a compiler error.

    (Incidentally, the function searches for the current page name inside
    the provided href string. If it's found it displays a <li
    style="list-style-image: url('images/arrow-right.gif');"> before the
    link. Otherwise, it displays an <li> without a bullet.)

    Do any of you know a function or something that I can wrap around this
    href to escape the string, regardless of how many single or
    double-quotes there are, before it is passed to the function?

    Thanks,
    --Diarmaid.
     
    Diarmaid McGleenan, Nov 10, 2004
    #1
    1. Advertising

  2. On 10 Nov 2004 06:37:45 -0800, Diarmaid McGleenan
    <> wrote:

    > Hi all. This isn't quite the same single/double-quote problem we see
    > posted a multitude of times in this ng. Read on...


    Actually, I think it is, but in any case it is the same question you
    posted yesterday. I won't repeat everything I wrote then so if you haven't
    done so already, please read my previous post.

    [snip]

    > Do any of you know a function or something that I can wrap around this
    > href to escape the string, regardless of how many single or
    > double-quotes there are, before it is passed to the function?


    This is a *syntax* error. No amount of function calls can correct that,
    only modification of the script itself. You will either have to instruct
    your content management system (which I believe you blamed yesterday) to
    output \" or \' when writing the string literal, or modify the output
    afterwards.

    Syntax has to be correct *before* it gets to the client. You can't correct
    it afterwards.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Nov 10, 2004
    #2
    1. Advertising

  3. Thanks for your suggestion Michael, and sorry about the double post
    for the same problem. I thought the other message didn't make it to
    the ng for whatever reason.

    --Diarmaid.

    >
    > > Hi all. This isn't quite the same single/double-quote problem we see
    > > posted a multitude of times in this ng. Read on...

    >
    > Actually, I think it is, but in any case it is the same question you
    > posted yesterday. I won't repeat everything I wrote then so if you haven't
    > done so already, please read my previous post.
    >
    > [snip]
    >
    > > Do any of you know a function or something that I can wrap around this
    > > href to escape the string, regardless of how many single or
    > > double-quotes there are, before it is passed to the function?

    >
    > This is a *syntax* error. No amount of function calls can correct that,
    > only modification of the script itself. You will either have to instruct
    > your content management system (which I believe you blamed yesterday) to
    > output \" or \' when writing the string literal, or modify the output
    > afterwards.
    >
    > Syntax has to be correct *before* it gets to the client. You can't correct
    > it afterwards.
    >
    > Mike
     
    Diarmaid McGleenan, Nov 11, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    13,432
  2. Eric Layman
    Replies:
    3
    Views:
    665
    Rad [Visual C# MVP]
    Apr 14, 2007
  3. Evan
    Replies:
    1
    Views:
    421
    Marc 'BlackJack' Rintsch
    Nov 4, 2008
  4. Evan
    Replies:
    3
    Views:
    554
    Marc 'BlackJack' Rintsch
    Nov 4, 2008
  5. vikrant
    Replies:
    8
    Views:
    393
    vikrant
    May 17, 2007
Loading...

Share This Page