javascript:pick doesn't work with the ' character

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Mayor

    javascript:pick doesn't work with the ' character

    Hi,
    If I have the ' character within the javascript:pick command it doesn't
    work. Is there some sort of way of escaping these characters like in
    server side languages.

    function pick(symbol) {
    if (window.opener && !window.opener. closed)
    window.opener.d ocument.create. text1.value = symbol;
    window.close();
    }

    <A HREF="javascrip t:pick('This doesn't work')">This doesn't work</A>

    It works fine as long as there is no ' in the pick bit. I need to be
    able to accept any characters in there as it's created by the users.

    Any ideas?

    Thanks

    Gary
  • Michael Winter

    #2
    Re: javascript:pick doesn't work with the ' character

    On Wed, 25 Feb 2004 15:34:33 +0000, Gary Mayor <gary@abertron. co.uk> wrote:
    [color=blue]
    > If I have the ' character within the javascript:pick command it doesn't
    > work. Is there some sort of way of escaping these characters like in
    > server side languages.
    >
    > function pick(symbol) {
    > if (window.opener && !window.opener. closed)
    > window.opener.d ocument.create. text1.value = symbol;
    > window.close();
    > }
    >
    > <A HREF="javascrip t:pick('This doesn't work')">This doesn't work</A>
    >
    > It works fine as long as there is no ' in the pick bit. I need to be
    > able to accept any characters in there as it's created by the users.
    >
    > Any ideas?[/color]

    Read the FAQ, specifically:



    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Gary Mayor

      #3
      Re: javascript:pick doesn't work with the ' character

      Michael Winter wrote:[color=blue]
      > On Wed, 25 Feb 2004 15:34:33 +0000, Gary Mayor <gary@abertron. co.uk> wrote:
      >[color=green]
      >> If I have the ' character within the javascript:pick command it
      >> doesn't work. Is there some sort of way of escaping these characters
      >> like in server side languages.
      >>
      >> function pick(symbol) {
      >> if (window.opener && !window.opener. closed)
      >> window.opener.d ocument.create. text1.value = symbol;
      >> window.close();
      >> }
      >>
      >> <A HREF="javascrip t:pick('This doesn't work')">This doesn't work</A>
      >>
      >> It works fine as long as there is no ' in the pick bit. I need to be
      >> able to accept any characters in there as it's created by the users.
      >>
      >> Any ideas?[/color]
      >
      >
      > Read the FAQ, specifically:
      >
      > http://jibbering.com/faq/#FAQ4_24
      >
      > Mike
      >[/color]

      Hi,
      Thanks for that i guess that means i have to do it using a function. How
      do you escape characters as a string in javascript. In perl I do it like
      this

      $a = qq{Some Stuff};

      does javascripts have a similar function that will work with my problem.

      Thanks

      Gary

      Comment

      • Michael Winter

        #4
        Re: javascript:pick doesn't work with the ' character

        On Wed, 25 Feb 2004 16:25:29 +0000, Gary Mayor <gary@abertron. co.uk> wrote:

        [snip]
        [color=blue]
        > How do you escape characters as a string in javascript. In perl I
        > do it like this
        >
        > $a = qq{Some Stuff};
        >
        > does javascripts have a similar function that will work with my problem.[/color]

        In literals, you use a backslash. In variables, it depends why you want to
        escape a string.

        The escape() and unescape() functions might be appropriate, but they are
        designed for escaping strings before being place in a URL. As such, some
        of the characters you might want escaped won't be, and some of the
        characters you don't want escaped will be. They also introduce some hassle
        with spaces: spaces are encoded as plus symbols (+), but plus symbols in
        the original string are left intact. When unescaped, all plus symbols
        (including those that were spaces) still remain in the string. If the
        original string did contain plus symbols, you won't be able to perform a
        simple search and replace.

        Experiment. If they suit your needs, then fine. If not, then you'll have
        to explain in more detail.

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Robert

          #5
          Re: javascript:pick doesn't work with the ' character

          The original problem is that javascript sees:
          pick('This doesn't work')

          Michael's suggestion would give:
          pick(unescape(' This%20doesn%27 t%20work'))


          In article <opr3xp7wwe5vkl cq@news-text.blueyonder .co.uk>,
          Michael Winter <M.Winter@bluey onder.co.invali d> wrote:
          [color=blue]
          > On Wed, 25 Feb 2004 16:25:29 +0000, Gary Mayor <gary@abertron. co.uk> wrote:
          >
          > [snip]
          >[color=green]
          > > How do you escape characters as a string in javascript. In perl I
          > > do it like this
          > >
          > > $a = qq{Some Stuff};
          > >
          > > does javascripts have a similar function that will work with my problem.[/color]
          >
          > In literals, you use a backslash. In variables, it depends why you want to
          > escape a string.
          >
          > The escape() and unescape() functions might be appropriate, but they are
          > designed for escaping strings before being place in a URL. As such, some
          > of the characters you might want escaped won't be, and some of the
          > characters you don't want escaped will be. They also introduce some hassle
          > with spaces: spaces are encoded as plus symbols (+), but plus symbols in
          > the original string are left intact.[/color]

          The escape and unescape functions are used in common cookie routines.
          I'd hope the round trip would be OK. Has this changed over time or in
          different browsers?

          In my two tests, I saw the space character changed to a %20. The plus
          character wasn't changed.



          Here is my test file:
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html;
          charset=windows-1252">
          <title>Rememb er by cookie</title>


          <script type="text/javascript">
          function beforePick(inDa ta) {

          pick(escape(inD ata));
          pick(unescape(e scape(inData))) ;
          }

          function pick(symbol) {
          alert("symbol = " + symbol);
          }
          </SCRIPT>
          <BODY>
          <p>Let's try some escapes in the MacOS of Netscape 7.1.and IE 5.2.</p>

          <script type="text/javascript">
          var all = "I hope this quote doesn't crumble.";
          var moreAll = "A bunch of troublesome characters: <br> <\/br> ;
          +_\\{}[]";
          document.write( "<p>");
          document.write( "escape(all ) = " + escape(all) +"<\/br>");
          document.write( "escape(moreAll ) = " + escape(moreAll) +"<\/br>");
          document.write( "</p>");
          var data = 'This doesn\'t work';
          beforePick(data )
          document.write( "escape(dat a) = " + escape(data));
          </SCRIPT>
          </body>
          </html>

          Comment

          • Richard Cornford

            #6
            Re: javascript:pick doesn't work with the ' character

            Robert <c6bb75ff.04022 52008.1a925913@ posting.google. com> wrote:
            [top-post fixed]

            Pleas do not top-post to comp.lang.javas cript. The group FAQ outlines
            acceptable posting style in section 2.3 paragraph 5 and references the
            applicable standard.
            [color=blue]
            >Michael Winter wrote:[color=green]
            >> Gary Mayor wrote:[/color][/color]
            <snip>[color=blue][color=green][color=darkred]
            >>> does javascripts have a similar function that will
            >>> work with my problem.[/color]
            >>
            >> In literals, you use a backslash. ...[/color][/color]
            <snip>
            [color=blue]
            > The original problem is that javascript sees:
            > pick('This doesn't work')
            >
            > Michael's suggestion would give:
            > pick(unescape(' This%20doesn%27 t%20work'))[/color]

            In the context of the OP's original code Michael appears to me to be
            endorsing a backslash escape:-

            onclick="pick(' This doesn\'t work');return false;"

            The rest of his post seems to be additional information on some of the
            escaping functions available in javascript for comparison with perl's.

            Richard.


            Comment

            Working...