Skip to Content

socialSharing()

Create social sharing URLs on specific links.

JavaScript February 14, 2019

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Additional Notes

This function looks for the classes fbshare (Facebook), twshare (Twitter), lishare (LinkedIn), pinshare (Pinterest), and emshare (Email) to replace the href with their appropriate “sharer” intent URL (and parameters).

Source File

Located in /assets/js/nebula.js on line 624.

JavaScript
nebula.socialSharing = function(){
    if ( jQuery('.fbshare, a.nebula-share.facebook, .twshare, a.nebula-share-btn.twitter, .lishare, a.nebula-share-btn.linkedin, .pinshare, a.nebula-share-btn.pinterest, .emshare, a.nebula-share-btn.email, a.nebula-share.webshare, a.nebula-share.shareapi').length ){ //If any of the Nebula sharing classes are used
        var encloc = encodeURIComponent(window.location.href);
        var enctitle = encodeURIComponent(document.title);
        var popupTop = nebula.dom.window.height()/2-275;
        var popupLeft = nebula.dom.window.width()/2-225;
        var popupAttrs = 'top=' + popupTop + ', left=' + popupLeft + ', toolbar=0, location=0, menubar=0, directories=0, scrollbars=0, chrome=yes, personalbar=0';

        //Facebook
        jQuery('.fbshare, a.nebula-share.facebook').attr('href', 'http://www.facebook.com/sharer.php?u=' + encloc + '&t=' + enctitle).attr({'target': '_blank', 'rel': 'noopener'}).on('click', function(e){
            var thisEvent = {
                event: e,
                category: 'Social',
                action: 'Share',
                intent: 'Intent',
                network: 'Facebook',
                url: window.location.href,
                title: document.title
            };

            ga('set', nebula.analytics.dimensions.eventIntent, thisEvent.intent);
            nebula.dom.document.trigger('nebula_event', thisEvent);
            ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.network);
            nebula.nv('event', thisEvent.network + ' ' + thisEvent.action);

            if ( nebula.dom.body.hasClass('desktop') ){
                window.open(jQuery(this).attr('href'), 'facebookShareWindow', 'width=550, height=450, ' + popupAttrs);
                return false;
            }
        });

        //Twitter
        jQuery('.twshare, a.nebula-share-btn.twitter').attr('href', 'https://twitter.com/intent/tweet?url=' + encloc + '&text=' + enctitle).attr({'target': '_blank', 'rel': 'noopener'}).on('click', function(e){
            var thisEvent = {
                event: e,
                category: 'Social',
                action: 'Share',
                intent: 'Intent',
                network: 'Twitter',
                url: window.location.href,
                title: document.title
            };

            ga('set', nebula.analytics.dimensions.eventIntent, thisEvent.intent);
            nebula.dom.document.trigger('nebula_event', thisEvent);
            ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.network);
            nebula.nv('event', thisEvent.network + ' ' + thisEvent.action);

            if ( nebula.dom.body.hasClass('desktop') ){
                window.open(jQuery(this).attr('href'), 'twitterShareWindow', 'width=600, height=254, ' + popupAttrs);
                return false;
            }
        });

        //LinkedIn
        jQuery('.lishare, a.nebula-share-btn.linkedin').attr('href', 'http://www.linkedin.com/shareArticle?mini=true&url=' + encloc + '&title=' + enctitle).attr({'target': '_blank', 'rel': 'noopener'}).on('click', function(e){
            var thisEvent = {
                event: e,
                category: 'Social',
                action: 'Share',
                intent: 'Intent',
                network: 'LinkedIn',
                url: window.location.href,
                title: document.title
            };

            ga('set', nebula.analytics.dimensions.eventIntent, thisEvent.intent);
            nebula.dom.document.trigger('nebula_event', thisEvent);
            ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.network);
            nebula.nv('event', thisEvent.network + ' ' + thisEvent.action);

            if ( nebula.dom.body.hasClass('desktop') ){
                window.open(jQuery(this).attr('href'), 'linkedinShareWindow', 'width=600, height=473, ' + popupAttrs);
                return false;
            }
        });

        //Pinterest
        jQuery('.pinshare, a.nebula-share-btn.pinterest').attr('href', 'http://pinterest.com/pin/create/button/?url=' + encloc).attr({'target': '_blank', 'rel': 'noopener'}).on('click', function(e){
            var thisEvent = {
                event: e,
                category: 'Social',
                action: 'Share',
                intent: 'Intent',
                network: 'Pinterest',
                url: window.location.href,
                title: document.title
            };

            ga('set', nebula.analytics.dimensions.eventIntent, thisEvent.intent);
            nebula.dom.document.trigger('nebula_event', thisEvent);
            ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.network);
            nebula.nv('event', thisEvent.network + ' ' + thisEvent.action);

            if ( nebula.dom.body.hasClass('desktop') ){
                window.open(jQuery(this).attr('href'), 'pinterestShareWindow', 'width=600, height=450, ' + popupAttrs);
                return false;
            }
        });

        //Email
        jQuery('.emshare, a.nebula-share-btn.email').attr('href', 'mailto:?subject=' + enctitle + '&body=' + encloc).attr({'target': '_blank', 'rel': 'noopener'}).on('click', function(e){
            var thisEvent = {
                event: e,
                category: 'Social',
                action: 'Share',
                intent: 'Intent',
                network: 'Email',
                url: window.location.href,
                title: document.title
            };

            ga('set', nebula.analytics.dimensions.eventIntent, thisEvent.intent);
            nebula.dom.document.trigger('nebula_event', thisEvent);
            ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.network);
            nebula.nv('event', thisEvent.network + ' ' + thisEvent.action);
        });

        //Web Share API: https://caniuse.com/mdn-api_navigator_share
        if ( 'share' in navigator && !nebula.dom.body.hasClass('desktop') ){ //Chrome 61+
            nebula.dom.document.on('click', 'a.nebula-share.webshare, a.nebula-share.shareapi', function(){
                var oThis = jQuery(this);
                
                navigator.share({
                    title: document.title,
                    text: nebula.post.excerpt,
                    url: window.location.href
                }).then(function(){
                    var thisEvent = {
                        event: e,
                        category: 'Social',
                        action: 'Share',
                        intent: 'Intent',
                        network: 'Web Share API',
                        url: window.location.href,
                        title: document.title,
                    };

                    nebula.dom.document.trigger('nebula_event', thisEvent);
                    ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.network);
                    nebula.nv('event', thisEvent.network);
                    oThis.addClass('success');
                    nebula.createCookie('shareapi', true);
                }).catch(function(error){
                    ga('send', 'exception', {'exDescription': '(JS) Share API Error: ' + error, 'exFatal': false});
                    oThis.addClass('error').text('Sharing Error');
                    nebula.createCookie('shareapi', false);
                });

                return false;
            });

            nebula.createCookie('shareapi', true); //Set a cookie to speed up future page loads by not loading third-party share buttons.
        } else {
            jQuery('a.nebula-share.webshare, a.nebula-share.shareapi').addClass('hidden');
        }
    }
};

Override

To override or disable this JavaScript function, simply redeclare it with the exact same function name.

JavaScript
nebula.socialSharing = function(){
    //Write your own code here, leave it blank, or return false.
}