Skip to content

Instantly share code, notes, and snippets.

@bbrewer97202
Created February 20, 2014 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbrewer97202/9120300 to your computer and use it in GitHub Desktop.
Save bbrewer97202/9120300 to your computer and use it in GitHub Desktop.
Simple Facebook,Twitter and Google+ social sharing calls
<!doctype html>
<html>
<head>
<title>Social share page title</title>
<meta name="description" content="Social share page description" />
<meta property="og:title" content="Social share og:title value" />
<meta property="og:description" content="Social share og:description value" />
<meta property="og:image" content = "http://www.thunderfarm.com/socialshare/socialshare.jpg" />
</head>
<body>
<button id="facebook">Facebook</button>
<button id="twitter">Twitter</button>
<button id="googleplus">Google+</button>
<script type="text/javascript">
var SocialShare = (function() {
function init() {
var baseURL = window.location.href;
//facebook
document.getElementById('facebook').addEventListener('click', function() {
var url = "https://www.facebook.com/sharer.php?u=" + encodeURIComponent(baseURL);
openWindow(url);
});
//twitter
document.getElementById('twitter').addEventListener('click', function() {
//use meta og:title for tweet
var tweet = "";
var metatags = document.getElementsByTagName('meta');
for (i=0; i < metatags.length; i++) {
if (metatags[i].getAttribute("property") === "og:title") {
tweet = metatags[i].getAttribute("content");
break;
}
}
var url = "http://twitter.com/intent/tweet?text=" + encodeURIComponent(tweet) + "&url=" + baseURL;
openWindow(url);
});
//googleplus
document.getElementById('googleplus').addEventListener('click', function() {
var url = "https://plus.google.com/share?url=" + encodeURIComponent(baseURL);
openWindow(url);
});
}
function openWindow(url) {
var windowParams = 'height=400,width=640,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=yes,directories=yes,status=yes';
window.open(url, 'socialShare', windowParams);
}
return {
init: init
}
})();
SocialShare.init();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment