Function to make strings javascript friendly

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Billy Boone

    #1

    Function to make strings javascript friendly

    I want to put some dynamic text into some href's on my form:

    <a href="<url>" onMouseOver="wi ndow.status='<d ynamic text>'; return true"
    onMouseOut="win dow.status=''; return true>

    The dynamic text will from time to time have single quotes or double quotes
    in them - so before writing a function to make these dynamic strings work -
    wanted to check to see if such a function exist (that will handle any
    troublesome characters).

    Any insight/pointers you could provide - much appreciated.

    BBB


  • Daniel Kirsch

    #2
    Re: Function to make strings javascript friendly

    Billy Boone wrote:[color=blue]
    > I want to put some dynamic text into some href's on my form:
    >
    > <a href="<url>" onMouseOver="wi ndow.status='<d ynamic text>'; return true"
    > onMouseOut="win dow.status=''; return true>
    >
    > The dynamic text will from time to time have single quotes or double quotes
    > in them - so before writing a function to make these dynamic strings work -
    > wanted to check to see if such a function exist (that will handle any
    > troublesome characters).
    >
    > Any insight/pointers you could provide - much appreciated.[/color]

    You may try escape() and unescape()
    Or for newer UAs encodeURICompon ent() and decodeURICompon ent()

    Daniel

    Comment

    • Billy Boone

      #3
      Re: Function to make strings javascript friendly

      escape won't work (at least from what I've read) - need the text to be human
      readable
      I Am Here

      Not:

      I%20Am%20Here.


      "Daniel Kirsch" <Iwillnotread_d aniel@gmx.de> wrote in message
      news:d86ovs$7vr $02$2@news.t-online.com...[color=blue]
      > Billy Boone wrote:[color=green]
      > > I want to put some dynamic text into some href's on my form:
      > >
      > > <a href="<url>" onMouseOver="wi ndow.status='<d ynamic text>'; return[/color][/color]
      true"[color=blue][color=green]
      > > onMouseOut="win dow.status=''; return true>
      > >
      > > The dynamic text will from time to time have single quotes or double[/color][/color]
      quotes[color=blue][color=green]
      > > in them - so before writing a function to make these dynamic strings[/color][/color]
      work -[color=blue][color=green]
      > > wanted to check to see if such a function exist (that will handle any
      > > troublesome characters).
      > >
      > > Any insight/pointers you could provide - much appreciated.[/color]
      >
      > You may try escape() and unescape()
      > Or for newer UAs encodeURICompon ent() and decodeURICompon ent()
      >
      > Daniel[/color]


      Comment

      • Daniel Kirsch

        #4
        Re: Function to make strings javascript friendly

        Billy Boone wrote:[color=blue]
        > escape won't work (at least from what I've read) - need the text to be human
        > readable[/color]

        Well, if your string is allready a JavaScript string, it should work
        without any further changes to the string.

        If not, you may show us a concrete example of what you have, what you
        get and what you want.

        Daniel

        Comment

        • Matt Kruse

          #5
          Re: Function to make strings javascript friendly

          Billy Boone wrote:[color=blue]
          > I want to put some dynamic text into some href's on my form:
          > <a href="<url>" onMouseOver="wi ndow.status='<d ynamic text>'; return
          > true" onMouseOut="win dow.status=''; return true>
          > The dynamic text will from time to time have single quotes or double
          > quotes in them - so before writing a function to make these dynamic
          > strings work - wanted to check to see if such a function exist (that
          > will handle any troublesome characters).[/color]

          You need to do this on the server-side.

          Do your normal HTML-escaping (if the code will be within an HTML tag as
          above) then escape the following:
          ' to \'
          " to \"
          (newline) to \n

          That should cover it, and the text will then be safe enough to put within
          your quoted string.

          I did this in Struts, for example, by making my own <bean:write> tag which
          took another argument of javascriptEscap e="true" to do these conversions.

          --
          Matt Kruse



          Comment

          • Billy Boone

            #6
            Re: Function to make strings javascript friendly

            I'm getting info from a database:

            So I may have a string:

            Today's date is 6/8/2005

            So the single quote will mess things up. Or the string could come with a
            double quote in it.



            "Daniel Kirsch" <Iwillnotread_d aniel@gmx.de> wrote in message
            news:d86r2k$sm7 $04$1@news.t-online.com...[color=blue]
            > Billy Boone wrote:[color=green]
            > > escape won't work (at least from what I've read) - need the text to be[/color][/color]
            human[color=blue][color=green]
            > > readable[/color]
            >
            > Well, if your string is allready a JavaScript string, it should work
            > without any further changes to the string.
            >
            > If not, you may show us a concrete example of what you have, what you
            > get and what you want.
            >
            > Daniel[/color]


            Comment

            • Martin Honnen

              #7
              Re: Function to make strings javascript friendly



              Billy Boone wrote:
              [color=blue]
              > I want to put some dynamic text into some href's on my form:
              >
              > <a href="<url>" onMouseOver="wi ndow.status='<d ynamic text>'; return true"
              > onMouseOut="win dow.status=''; return true>
              >
              > The dynamic text will from time to time have single quotes or double quotes
              > in them - so before writing a function to make these dynamic strings work -
              > wanted to check to see if such a function exist (that will handle any
              > troublesome characters).[/color]

              If you are generating the dynamic text on the server then it is a server
              side problem where the solution depends on the scripting language used
              on the server.
              Are you using JavaScript on the server? Otherwise ask in a group about
              the server side language of your choice.


              --

              Martin Honnen
              http://JavaScript.FAQTs.com/

              Comment

              • ASM

                #8
                Re: Function to make strings javascript friendly

                Billy Boone wrote:[color=blue]
                > I want to put some dynamic text into some href's on my form:[/color]

                don't know what is "dynanic" text
                [color=blue]
                > <a href="<url>" onMouseOver="wi ndow.status='<d ynamic text>'; return true"
                > onMouseOut="win dow.status=''; return true>[/color]

                onMouseOut="win dow.status=''; return true;">

                and p e ?
                onMouseOver="wi ndow.status=<dy namic text>; return true;"


                --
                Stephane Moriaux et son [moins] vieux Mac

                Comment

                • fox

                  #9
                  Re: Function to make strings javascript friendly



                  Billy Boone wrote:[color=blue]
                  > I want to put some dynamic text into some href's on my form:
                  >
                  > <a href="<url>" onMouseOver="wi ndow.status='<d ynamic text>'; return true"
                  > onMouseOut="win dow.status=''; return true>
                  >
                  > The dynamic text will from time to time have single quotes or double quotes
                  > in them - so before writing a function to make these dynamic strings work -
                  > wanted to check to see if such a function exist (that will handle any
                  > troublesome characters).
                  >
                  > Any insight/pointers you could provide - much appreciated.
                  >
                  > BBB
                  >[/color]


                  String.prototyp e.addslashes = function()
                  {
                  return this.replace(/([\'\"])/g,"\\$1");
                  }


                  Comment

                  Working...