user847495 user847495 - 1 year ago 64
Javascript Question

How do you pass a single quote through a URL?

I'm using Node.js:

var s = 'Who\'s that girl?';
var url = '' + encodeURIComponent(s);

request( url, POST, ...)

This does not work! And Facebook cuts off my text...

Full code:

function postToFacebook(fbid, access_token, data, next){
var uri = ''+String(fbid)+'/feed?access_token='+access_token;
var uri += '&' + querystring.stringify(data);
'uri': uri,

var d = {
'name':'Who\'s that girl?',
'link': '',
'caption': 'some caption...',
'description': 'some description...',
'picture': '',
postToFacebook(, req.user.fb.accessToken, d);

Facebook gets a blank post on the wall. No text shows. Nothing.

When I log my URI, it is this:|d7ae6f314515c918732eab36.1-1230602668|GtOJ-pi3ZBatd41tPvrHb0OIYyk&name=Who's%20that%20girl%3F&

Obviously if you take a look at that URL, you see that the single quote is not being encoded correctly.

Answer Source

I'm doing a similar thing (also with Node.js) and first tried using JavaScript's built-in escape() function, but it didn't really work.

Here's how I ended up getting search to work. It might just be a fluke:

 function doMySearch(showTitle) {
     showTitle = escapeShowTitle(showTitle)
     var url = "" + showTitle + "&type=page"

function escapeShowTitle(title) {
    title = title.replace(/'/g, "")
    title = escape(title)
    return title

doMySearch("America's Funniest home Videos")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download