SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict RapidGaming's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple Escape Problem

    I have this:

    Code:
    <script language="JavaScript"><!--
     var str = '{param}'; var sArray = str.split("%"); rand = Math.floor(Math.random() * sArray.length); document.write(sArray[rand]);
     // --></script>
    {param} cannot be controller, it is led by a php script.

    Let's say that:
    {param} = "Yes sir", said Mr. Jones%'How are you?' screamed Jennie%Hmmmm....

    Now, it has both " and ' in it, so I'm looking for a JavaScript solution to escaping or replacing ' with ` in the string. I can't do it with php as we can't edit that, it all has to be done in javascript.

    Hope you understand what's happening.
    Scott

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    str = str.replace( /'/g, "`" );
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Addict RapidGaming's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    str = str.replace( /'/g, "`" );
    I don't see how I can do it. Here is what I'm testing with:

    Code:
    <script language="JavaScript"><!--
     var str = '"number1"%number2%number3%testing this one's';
     str = str.replace( /'/g, "`" );
     var sArray = str.split("%"); rand = Math.floor(Math.random() * sArray.length); document.write(sArray[rand]);
     // --></script>

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh - I see what you're saying.

    Uhh - you can't do this without changing the PHP.

    But, you can do something like this

    HTML Code:
    <script type="text/javascript">
    var str = document.getElementById( "param" ).innerHTML;
    </script>
     
    <div id="param" style="display:none">{param}</div>
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Addict RapidGaming's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    Oh - I see what you're saying.

    Uhh - you can't do this without changing the PHP.

    But, you can do something like this

    HTML Code:
     <script type="text/javascript">
     var str = document.getElementById( "param" ).innerHTML;
     </script>
      
     <div id="param" style="display:none">{param}</div>
    I don't get what that would do either lol.

    What I want is like a random quote.

    {param} = random1%random2%random3 (% can be changed to something else) and it will randomly select one and write it. But I also want to get around the ' and " problems if possible please.

    I really hope there is a way around this.
    Thanks for the help so far,
    Scott.

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, here's what I'm telling you.

    If you are receiving a random value from PHP that is a string that may or may not contain both of the delimiting characters used for javascript strings (those being " and ') than there is no way for you to fix it every time by only using Javascript. Javascript is powerless to fix something that causes an error previous to it's execution. You will get "Unterminated String Constant" errors, which are are actually runtime errors, so no code will execute, even preceeding code.

    So, the issue of splicing that string and displaying a piece of it is moot until you can figure out how to reliably get that data into a Javascript variable. The way I see it, what I have shown is your only option that does not involve changing the PHP.

    The rest of your script looks good.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Addict RapidGaming's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ohh right, thanks I'll look into getting some PHP done.


Bookmarks

Posting Permissions

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