Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    84
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Arrow Facebook changes %20 to + HELP

    Hi there.

    Im using a great script to grab info from the url like ?title=My%20Title

    this is working great until i share the page on facebook then it comes back like: ?title=My+Title when clicked via Facebook. This is a problem because the title now reads My+Title instead of my Title as it should.


    heres what i Got:

    The Script that seperates the segmants in the url:
    Code:
    <script type="text/javascript">
    
    var qs = [ ];
    
    if ( location.search.length > 1 )
    {
        var pairs = location.search.substring(1).split("&");
        for ( var p = 0; p < pairs.length; ++p )
        {
            
    		var pair = pairs[p].split("=");
            qs[pair[0]] = unescape( pair[1] );
    		
        }
    	
    
    }
    
    var title = ""+ qs["title"] +"" ;
    var date = ""+ qs["date"] +"" ;
    var channel = ""+ qs["channel"] +"" ;
    var info = ""+ qs["info"] +"" ;
    var image = ""+ qs["image"] +"" ;
    var stream = ""+ qs["stream"] +"" ;
    // do the replacements *after* page is loaded, for simplicity:
    function doReplacements( )
    {
        var link = document.getElementById("link1");
        link.href = url;    
        link.innerHTML = qs["page"];
    
        
    } 
    window.onload = doReplacements;  
    </script>
    An example of how i use the info:
    Code:
    <script type="text/javascript">
    document.write(''+info+'');
    </script>

    This part shares the unique url on Facebook and allows me to have just one page with many URLS for facebok sharing and commenting:
    Code:
    <fb:comments href="<?php
    $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    echo $url;
    ?>" num_posts="4" width="620"></fb:comments>

    So what i need to do is be able to either deal with or change the + that comes back from facebook.

    Thank You

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    %20 and + are *BOTH* accepted encodings of a space character. So obviously what is happening is that Facebook sees your SPACE (which you encoded as %20) and, when it then later encodes it, encodes it instead as +.

    The answer is to stop using outdated escape() and unescape() and instead use encodeURIComponent() and decodeURIComponent()

    I *THINK* that is all you will need to do.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    84
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Arrow

    Quote Originally Posted by Old Pedant View Post
    %20 and + are *BOTH* accepted encodings of a space character. So obviously what is happening is that Facebook sees your SPACE (which you encoded as %20) and, when it then later encodes it, encodes it instead as +.

    The answer is to stop using outdated escape() and unescape() and instead use encodeURIComponent() and decodeURIComponent()

    I *THINK* that is all you will need to do.
    Thanks, any ideas how/where to begin!?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    ??? I thought I told you.

    For example, this line:
    Code:
        qs[pair[0]] = unescape( pair[1] );
    should probably just be
    Code:
        qs[pair[0]] = decodeURIComponent( pair[1] );
    This all looks suspiciously like my own code.

    qs[] and pairs, for example, are what I usually use when I answer questions about querystrings in this forum.

    And I probably did use unescape() (unadvisably) in the past.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    Ahh...shame on me. I should have tested my answer before replying!

    Indeed, decodeURIComponent() solves other problems, but it does *NOT* solve the + problem.

    Okay, so try this:
    Code:
        qs[pair[0]] = decodeURIComponent( pair[1] ).replace(/\+/g," ");
    That seemed to work in my testing.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    embeebutterly (07-17-2012)

  • #6
    New Coder
    Join Date
    Jan 2011
    Posts
    84
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Yes! It was you who helped me in the begining!

    That has done the job, +'s are gone, thank you!

    Now I dont suppose you could help me with this one ;-)

    http://www.codingforums.com/showthread.php?t=267686

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    Nope. I don't use jQuery. Good luck.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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