How Do You Pass An Apostrophe Through A Url?
Solution 1:
Had the same problem, encodeURIComponent didn't encode single quote. The trick is to do the replacement of ' with %27, after the encoding:
var trackArtistTitle = encodeURIComponent("Johnny Vegas - Who's Ready Fo'r Ice Cre'am")
// result: Johnny%20Vegas%20-%20Who's%20Ready%20Fo'r%20Ice%20Cre'am
trackArtistTitle = trackArtistTitle.replace(/'/g, '%27')
// result: Johnny%20Vegas%20-%20Who%27s%20Ready%20Fo%27r%20Ice%20Cre%27am
This way, trackArtistTitle will be properly decoded on server i.e. with PHP using urldecode().
Solution 2:
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:
functiondoMySearch(showTitle) {
showTitle = escapeShowTitle(showTitle)
var url = "http://graph.facebook.com/search?q=" + showTitle + "&type=page"doSomethingWith(url)
}
functionescapeShowTitle(title) {
title = title.replace(/'/g, "")
title = escape(title)
return title
}
doMySearch("America's Funniest home Videos")
Solution 3:
I know this doesn't address the OP's question, but for those coming here with OData Query related questions, note that the escape character is yet another single quote.
unescapedValue.replace(/'/g, '\'\'')
This assumes you have already performed an encodeURIComponent(unescapedValue)
on your string
Solution 4:
Recent answer (2021)
Using JavaScript's URLSearchParams
:
console.log(newURLSearchParams({ text: "Who's that girl?" }).toString())
// orconsole.log(newURLSearchParams("text=Who's that girl?").toString())
Post a Comment for "How Do You Pass An Apostrophe Through A Url?"