Created
February 20, 2014 18:36
-
-
Save bbrewer97202/9120300 to your computer and use it in GitHub Desktop.
Simple Facebook,Twitter and Google+ social sharing calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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; | |
document.getElementById('facebook').addEventListener('click', function() { | |
var url = "https://www.facebook.com/sharer.php?u=" + encodeURIComponent(baseURL); | |
openWindow(url); | |
}); | |
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