ellisonleao / sharer.js

:on: :bookmark: Create your own social share buttons. No jquery.
http://ellisonleao.github.io/sharer.js
MIT License
2.02k stars 197 forks source link

sharer.js Issue with ajax loaded content #45

Closed optimusvish closed 7 years ago

optimusvish commented 7 years ago

@ellisonleao Can you help me out? I'm using this script on my page but it's not working on ajax loaded page.

optimusvish commented 7 years ago

@ellisonleao I'm using one div and loading the content from another page to this div so the script is not working. If i use directly in a page it'll work but not on ajax loaded content.

ellisonleao commented 7 years ago

Do you have a piece of code so we can test here?

romettm commented 7 years ago

`/**

(function () { 'use strict'; /**

Call installSharer() after ajax content is loaded

optimusvish commented 7 years ago

@ellisonleao @romettm sorry guys I'm out of town for sometime. Anyway thanks for your replies. Actually I fixed the issue that day only. Thanks any way. and @ellisonleao thank you so much for the script (sharer.js) file. Using that I've written this code and I didn't include your js file. This is the code I used: $(document).on("click", '.sharer', function() { var sharer = $(this).attr("data-sharer"); var text = $(this).attr("data-title"); var url = $(this).attr("data-url"); var hashtags = $(this).attr("data-hashtags"); var image = ''; var description = text; var to = ''; if(sharer == 'whatsapp') { var message = encodeURIComponent(text) + " - " + encodeURIComponent(url); var location = "whatsapp://send?text=" + message; } else if(sharer == 'facebook') { var location = "https://www.facebook.com/sharer/sharer.php?u=" + url; } else if(sharer == 'twitter') { var location = "https://twitter.com/intent/tweet/?text="+text+"&url="+url+"&hashtags="+hashtags; } else if(sharer == 'googleplus') { var location = "https://plus.google.com/share?url="+url; } else if(sharer == 'linkedin') { var location = "https://www.linkedin.com/shareArticle?url="+url+'&mini=!0'; } else if(sharer == 'telegram') { var location = "tg://msg_url?text="+text+" "+url; } else if(sharer == 'pinterest') { var location = "https://www.pinterest.com/pin/create/button/?url="+url+"&media="+image+'&description='+description; } else if(sharer == 'email') { var location = "mailto:"+to+"&subject="+text+"&body="+text+"\n"+url; } window.location.href = location; });

ellisonleao commented 7 years ago

yea, the problem is that the bindings are added on window load, not click. so if you got another element which renders after that binding, the script will not work.

thanks for sharing the issue!