...

View Full Version : Facebook changes %20 to + HELP



embeebutterly
07-15-2012, 08:24 PM
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:



<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:

<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:

<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:thumbsup:

Old Pedant
07-15-2012, 09:49 PM
%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.

embeebutterly
07-15-2012, 10:25 PM
%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!?

Old Pedant
07-15-2012, 10:38 PM
??? I thought I told you.

For example, this line:


qs[pair[0]] = unescape( pair[1] );
should probably just be


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.

Old Pedant
07-15-2012, 10:42 PM
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:


qs[pair[0]] = decodeURIComponent( pair[1] ).replace(/\+/g," ");

That seemed to work in my testing.

embeebutterly
07-16-2012, 05:46 AM
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 ;-)

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

Old Pedant
07-16-2012, 07:46 PM
Nope. I don't use jQuery. Good luck.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum