0

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
3
  • and how about function encodeURI , it's good for urls for social shares ? Commented Sep 1, 2014 at 10:34
  • out of interest, how did you know about the replace function in the first place? Just wondering what source you have seen that doesn't show you the correct way to use it Commented Sep 1, 2014 at 10:36
  • its was my mistake , i didn't see it , need some rest... Commented Sep 1, 2014 at 10:37

2 Answers 2

5

.replace will return the new value back, so you need to save it to the variable:

share = share.replace( ... )
answered Sep 1, 2014 at 10:30
Sign up to request clarification or add additional context in comments.

Comments

0

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);
answered Sep 1, 2014 at 13:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.