I have the following code to share my website through some social networks when the user clicks some divs with the appropriate icons.

var loc = encodeURIComponent(document.location.href);
$(document).ready(function() {
  $("#shareFacebook").click(function() {
    window.open('https://www.facebook.com/sharer/sharer.php?u=' + loc, '_blank');
  $("#shareTwitter").click(function() {
    window.open('https://twitter.com/share?url=' + loc + '&text=myText', '_blank');
  $("#shareGooglePlus").click(function() {
    window.open('https://plus.google.com/share?url=' + loc, '_blank');
  $("#shareLinkedIn").click(function() {
    window.open('https://www.linkedin.com/shareArticle?mini=true&url=' + loc + '&title=mySite&summary=myText', '_blank');
  $("#sharePinterest").click(function() {
    window.open('https://pinterest.com/pin/create/button/?url=http://mysite.com/logo.png&media=Logo&description=myText', '_blank');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="shareFacebook">
  <div class="icon-facebook"></div><span>Facebook</span>
<div id="shareTwitter">
  <div class="icon-twitter"></div><span>Twitter</span>
<div id="shareGooglePlus">
  <div class="icon-google-plus"></div><span>Google+</span>
<div id="shareLinkedIn">
  <div class="icon-linkedin"></div><span>LinkedIn</span>
<div id="sharePinterest">
  <div class="icon-pinterest"></div><span>Pinterest</span>

(Even if the code works, for some reason, the snippet does not.)

Can it be made more efficient?

  • \$\begingroup\$ While the code is about as basic as jQuery / JavaScript gets, there's not much to improve in terms of performance here. \$\endgroup\$ – user3267755 Sep 2 '15 at 13:03
  • \$\begingroup\$ Then, should I close the question ordelete it? \$\endgroup\$ – VannTile Ianito Sep 2 '15 at 13:21
  • \$\begingroup\$ Anyone feel free to chime in, but I think it was a fair question to ask and that you would be okay with just closing it. \$\endgroup\$ – user3267755 Sep 2 '15 at 13:30
  • 2
    \$\begingroup\$ Having no room to improve doesn't really mean you should close. Closing is for questions that need editing to fit what's on topic for the site. This question fits. If no-one is able to answer you could opt to delete it if you'd like to. \$\endgroup\$ – SuperBiasedMan Sep 2 '15 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.