I'm using phonegap to share an article via WhatsApp.
The code for the button is as follows:
shareArticle += '<li class="rrssb-whatsapp"><a href="javascript: void(1)" onclick="window.plugins.socialsharing.shareViaWhatsApp(\''+$('.article_title').html().replace(/'/g, "'")+'\', null, \''+articleId+'\', function() {console.log(\'share ok\')}, function(errormsg){alert(errormsg)});" class="popup" data-action="share/whatsapp/share">';
shareArticle += '<span class="rrssb-icon"><!-- Icon in SVG --></span>';
shareArticle += '</a></li>';
The part that I'm asking about is this:
onclick="window.plugins.socialsharing.shareViaWhatsApp(\''+$('.article_title').html().replace(/'/g, "'")+'\', null, \''+articleId+'\', function() {console.log(\'share ok\')}, function(errormsg){alert(errormsg)});"
The button is not working when there is an apostrophe in the title.
The strangest thing is that if I replace '
with "
it work perfectly (even thought the result is wrong).
Doe's anybody has any idead why '
fails?
I'm using phonegap to share an article via WhatsApp.
The code for the button is as follows:
shareArticle += '<li class="rrssb-whatsapp"><a href="javascript: void(1)" onclick="window.plugins.socialsharing.shareViaWhatsApp(\''+$('.article_title').html().replace(/'/g, "'")+'\', null, \'http://www.myaddress./showArticle-'+articleId+'\', function() {console.log(\'share ok\')}, function(errormsg){alert(errormsg)});" class="popup" data-action="share/whatsapp/share">';
shareArticle += '<span class="rrssb-icon"><!-- Icon in SVG --></span>';
shareArticle += '</a></li>';
The part that I'm asking about is this:
onclick="window.plugins.socialsharing.shareViaWhatsApp(\''+$('.article_title').html().replace(/'/g, "'")+'\', null, \'http://www.myaddress./showArticle-'+articleId+'\', function() {console.log(\'share ok\')}, function(errormsg){alert(errormsg)});"
The button is not working when there is an apostrophe in the title.
The strangest thing is that if I replace '
with "
it work perfectly (even thought the result is wrong).
Doe's anybody has any idead why '
fails?
-
I doubt it works this way since you have two double quotes. One where the
onClick="
starts, and where you do areplace
("&apos")
. – putvande Commented Jul 17, 2015 at 15:16 -
Why not just
JSON.stringify(article_title)
to be sure it's properly encoded no matter what it is? – tadman Commented Jul 17, 2015 at 15:18 - @putvande If you'll look at the first example the string is escaped with a single quote, so the second double quote is not in the string. Anyway, I already tried changing it. Didn't make a difference. – Digi Commented Jul 17, 2015 at 15:18
- Yea I think there's some funky single/double quote early termination going on here. I just tried to work out where it was but it's too late on a Friday for my brain to function correctly! – Nathan Hornby Commented Jul 17, 2015 at 15:18
- @tadman because the function requires a string, not a JSON. – Digi Commented Jul 17, 2015 at 15:20
2 Answers
Reset to default 4Thank you all for your support.
The solution is to change the apostrophe to another sign that doesn't break the string.
So what I did is:
$('.opinion_content_title').html().replace(/'/g, "′")
Again, thank you all.
The named character reference
&apos
; (the apostrophe, U+0027) was introduced in XML 1.0 but does not appear in HTML. Authors should therefore use'
; instead of&apos
; to work as expected in HTML 4 user agents.