Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-15-2012, 08:24 PM   PM User | #1
embeebutterly
New Coder

 
Join Date: Jan 2011
Posts: 64
Thanks: 7
Thanked 0 Times in 0 Posts
embeebutterly is an unknown quantity at this point
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
embeebutterly is offline   Reply With Quote
Old 07-15-2012, 09:49 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
%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.
Old Pedant is offline   Reply With Quote
Old 07-15-2012, 10:25 PM   PM User | #3
embeebutterly
New Coder

 
Join Date: Jan 2011
Posts: 64
Thanks: 7
Thanked 0 Times in 0 Posts
embeebutterly is an unknown quantity at this point
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!?
embeebutterly is offline   Reply With Quote
Old 07-15-2012, 10:38 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
??? 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.
Old Pedant is offline   Reply With Quote
Old 07-15-2012, 10:42 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
embeebutterly (07-17-2012)
Old 07-16-2012, 05:46 AM   PM User | #6
embeebutterly
New Coder

 
Join Date: Jan 2011
Posts: 64
Thanks: 7
Thanked 0 Times in 0 Posts
embeebutterly is an unknown quantity at this point
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
embeebutterly is offline   Reply With Quote
Old 07-16-2012, 07:46 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:10 PM.


Advertisement
Log in to turn off these ads.