How can i rerender Pinterest's Pin It button?

前端 未结 13 2430
暖寄归人
暖寄归人 2021-01-30 18:14

I\'m trying to create and manipulate the Pin It button after page load. When i change the button properties with js, it should be rerendered to get the functionality of pinning

13条回答
  •  天命终不由人
    2021-01-30 18:50

    Here is what i did.. A slight modification on @Derrick Grigg to make it work on multiple pinterest buttons on the page after an AJAX reload.

    refreshPinterestButton = function () {
        var url, media, description, pinJs, href, html, newJS, js;
        var pin_url;
        var pin_buttons = $('div.pin-it a');
        pin_buttons.each(function( index ) {
            pin_url = index.attr('href');
            url = escape(getUrlVars(pin_URL)["url"]);
            media = escape(getUrlVars(pin_URL)["media"]);
            description = escape(getUrlVars(pin_URL)["description"]);
            href = 'http://pinterest.com/pin/create/button/?url=' + url + '&media=' + media + '&description=' + description;
            html = '';
            index.parent().html(html);
        });
    
        //remove and add pinterest js
        pinJs = '//assets.pinterest.com/js/pinit.js';
        js = $('script[src*="assets.pinterest.com/js/pinit.js"]');
        js.remove();
        js = document.createElement('script');
        js.src = pinJs;
        js.type = 'text/javascript';
        document.body.appendChild(js);
    }
    
    });
    
    
    function getUrlVars(pin_URL)
    {
        var vars = [], hash;
        var hashes = pin_URL.slice(pin_URL.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }
    

提交回复
热议问题