最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Replacing apostrophe in string - Stack Overflow

programmeradmin1浏览0评论

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, "&apos;")+'\', 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, "&apos;")+'\', 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 &apos; with &quot; it work perfectly (even thought the result is wrong).

Doe's anybody has any idead why &apos; 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, "&apos;")+'\', 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, "&apos;")+'\', 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 &apos; with &quot; it work perfectly (even thought the result is wrong).

Doe's anybody has any idead why &apos; fails?

Share Improve this question asked Jul 17, 2015 at 15:12 DigiDigi 1632 silver badges15 bronze badges 6
  • I doubt it works this way since you have two double quotes. One where the onClick=" starts, and where you do a replace ("&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
 |  Show 1 more ment

2 Answers 2

Reset to default 4

Thank 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 &#39; instead of &apos; to work as expected in HTML 4 user agents.

发布评论

评论列表(0)

  1. 暂无评论