Skip to content

Instantly share code, notes, and snippets.

@joewanko
Last active April 7, 2017 08:53
Show Gist options
  • Save joewanko/29fcac47d0152331b8de to your computer and use it in GitHub Desktop.
Save joewanko/29fcac47d0152331b8de to your computer and use it in GitHub Desktop.
Javascript functions that create Facebook and Twitter pop ups for sharing. No APIs are necessary.
'use strict';
/**
* Popup with a Facebook share page without the need of the JS API.
* @param {string} url URL of the page to share.
* @param {?number} opt_height Height of the popup.
* @param {?number} opt_width Width of the popup.
*/
facebookShare = function(url, opt_height, opt_width) {
var href = url;
var height = opt_height ? 400;
var width = opt_width ? 575;
var left = (window.outerWidth - width) / 2;
var top = (window.outerHeight - height) / 2;
var url = 'https://www.facebook.com/sharer/sharer.php?u=' + href;
var opts = 'status=1' +
',width=' + width +
',height=' + height +
',top=' + top +
',left=' + left;
window.open(url, 'Facebook', opts);
};
/**
* Creates a popup with a Twitter Tweet dialogue sharing a URL.
* @param {string} href URL of the page to share.
* @param {string} text Editable tweet text to include.
* @param {string} via Twitter user to attribute the URL to.
* @param {?number} opt_height Height of the popup.
* @param {?number} opt_width Width of the popup.
*/
twitterShare = function(href, text, via, opt_height, opt_width) {
var height = opt_height ? 400;
var width = opt_width ? 575;
var left = (window.outerWidth - width) / 2;
var top = (window.outerHeight - height) / 2;
var url = 'https://twitter.com/share?text=' + text +
'&url=' + href + '&via=' + via;
var opts = 'status=1' +
',width=' + width +
',height=' + height +
',top=' + top +
',left=' + left;
window.open(url, 'Twitter', opts);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment