UGH! Using escape characters don't work...anyone know why?

Discussion in 'Javascript' started by KathyB, Oct 11, 2003.

  1. KathyB

    KathyB Guest

    Hi, not sure this is the right group, but hoping someone may have
    experienced this.

    I'm passing html text as a parameter to a javascript. When it has an
    apostrophe in it, of course it does parse correctly. BTW, using IE6
    and msxml3/4.

    Even if I manually escape the ' with \' or '' the browser just sees it
    as \' or '' and not as an escaped apostrophe.

    This is driving me nuts! Everyone just tells me to escape the
    character.

    Any clues most welcome!

    Thanks, Kathy
     
    KathyB, Oct 11, 2003
    #1
    1. Advertisements

  2. KathyB

    Lee Guest

    KathyB said:
    You need to provide more detail. A link to a page or a small sample
    of code that shows the problem would help.
    Did you mean to say "of course it does NOT parse correctly" ?

    How are you passing the HTML text?

    Escape characters have no meaning in HTML.
    Escape characters only have meaning in literal text.
    They are ignored in text that is already in the value of a variable.
    For example, the following code will produce a page reading:

    Kathy\'s problem
    Kathy\'s problem
    Kathy's problem

    <html>
    <body>
    <div id="alpha">Kathy\'s problem</div>
    <script type="text/javascript">
    document.write(document.getElementById("alpha").innerHTML);
    document.write("<br>Kathy\'s problem");
    </script>
    </body>
    </html>


    <html>
    <body>
    <div id="alpha">Kathy\'s problem</div>
    <script type="text/javascript">
    document.write(document.getElementById("alpha").innerHTML);
    document.write("<br>Kathy\'s problem");
    </script>
    </body>
    </html>

    Kathy\'s problem
    Kathy\'s problem
    Kathy's problem
     
    Lee, Oct 11, 2003
    #2
    1. Advertisements

  3. KathyB

    Jim Ley Guest

    If it parses correctly, what's your problem...

    You might try actually illustrating your problem with code, or a link,
    you're not even telling us how you're "passing html text as a
    parameter to a javascript"

    Jim.
     
    Jim Ley, Oct 11, 2003
    #3
  4. KathyB

    Kathy Burke Guest

    Yes, I DID mean that it does NOT parse correctly. Here is an example of
    my script and parameter passed.

    <script language="Javascript">
    function Anomaly(textIn)
    {
    newWindow = window.open(('Anomaly.aspx?desc=' +textIn), 'Anomaly',
    'width=650,height=700');
    }
    </script>

    <input type="button" value="Anomaly"
    onclick="Javascript:Anomaly('Kathy's apostrophe test')">

    QUESTION: What does the parameter have to be to not throw an exception
    within the script? I've tried \' but it just results in \' in the html
    output not just the html source.

    I hope this clarifies my question. Thanks for responding!

    Kathy
     
    Kathy Burke, Oct 11, 2003
    #4
  5. ^
    The problem is here: ^
    That single-quote/apostrophe must be escaped.
    The onclick event should be:
    onclick="Anomaly('Kathy\'s apostrophe test')">

    Drop the "Javascript:". It doesn't belong there, and it makes no
    difference.


    Other comments:
    This should be
    Not all characters are allowed in URL's, so I would suggest changing
    "textIn" to "escape(textIn)". That will write characters that are
    not allowed in URL's as, e.g., %20 (a space).

    /L
     
    Lasse Reichstein Nielsen, Oct 11, 2003
    #5
  6. KathyB

    Allan W Guest

    (KathyB) wrote

    I think what you're asking is how to nest quotes. For instance, if you
    want to use document.write() to write out a statement that has a quote
    in it.

    If so, the answer is that most places that need quotes, can use either
    single quotes or double quotes.

    document.write("Kathy's page"); // Quoted string contains
    apostrophe
    shows up as
    Kathy's page

    document.write('Is "Kathy" home?"); // Quoted string contains
    quote
    shows up as
    Is "Kathy" home?

    If you find that you need to use both, you can use the + sign to
    concatenate.

    document.write('Is this "Kathy' + "'s Page" +'"?');
    shows up as
    Is this "Kathy's Page"?

    HTH
     
    Allan W, Oct 12, 2003
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.