why javascript function replace wont work ?
social_share: function(e){
var is_video,
social_name = $(e).attr('class'),
share_url = document.URL,
share_title = $('title').text(),
share_media,
share;
switch(social_name) {
case 'twitter':
share = 'https://twitter.com/share?url={share_url}&text={share_title}';
break;
case 'facebook':
share = 'https://www.facebook.com/dialog/feed?app_id=123050457758183&link={share_url}&picture={share_media}&name={share_title}';
break;
case 'google':
share = 'https://plus.google.com/share?url={share_url}';
break;
case 'pinterest':
share = 'https://pinterest.com/pin/create/bookmarklet/?media={share_media}&url={share_url}&is_video={is_video}&description={share_title}';
break;
case 'mailto':
share = '...';
break;
}
share.replace('{share_title}', share_title)
.replace('{share_url}', encodeURI(share_url))
.replace('{share_media}', encodeURI(share_media))
.replace('{is_video}', is_video);
console.log(share);
},
and console.log function return string share without any replaces... it would by https://twitter.com/share?url={share_url}&text={share_title} if twitter , and same other's
asked Sep 1, 2014 at 10:29
feesar
5002 gold badges5 silver badges21 bronze badges
2 Answers 2
Strings are immutable in javascript. So changing the string by any of the string function will return a new string as the output. In the above code change the syntax as :
share = share.replace('{share_title}', share_title);
Comments
lang-js
replacefunction in the first place? Just wondering what source you have seen that doesn't show you the correct way to use it