SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Adding escape characters to unescaped strings from server-side? (No server access)

    So I need to add escape characters (/) to the following:

    Code:
    var post_title = 'STRING FROM SERVER-SIDE GOES HERE';
    The reason I need to escape that string is because it might contain a single-quote (') or double-quote ("), which would obviously cause errors. Unfortunately, I do not have access to the server-side code, and it's unlikely that they're going to create a method to output escaped content anytime soon. (We're talking Blogger.)

    Can you think of a clever way to do this?

  2. #2
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that the best method is to put this text into the tag with display:none; style and then load this string from it using DOM methods.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @alex.w747. That would still trigger a JavaScript error...

    @micropat, there is encodeURI, but this would still fail if you write out the string from the server. You simply need to escape it on the server.

  4. #4
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure you try what I suggested? :-)

    The following should better describe my idea:
    Code:
    ...
    <div style="display:none;" id="my-text">STRING FROM SERVER-SIDE GOES HERE</div>
    <script type="text/javascript">
    var post_title = document.getElementById('my-text').innerHTML;
    </script>
    ...
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, that would work, yes.

    Smart alex.w747


  6. #6
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    I think that the best method is to put this text into the tag with display:none; style and then load this string from it using DOM methods.
    And... drum-roll for alex.w747! That is precisely the clever idea I was hoping for. So brilliantly simple. Thanks!


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
  •