How can i rerender Pinterest's Pin It button?

前端 未结 13 2140
暖寄归人
暖寄归人 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:44

    The official way to do this is by setting the "data-pin-build" attribute when loading the script:

    <script defer="defer" src="//assets.pinterest.com/js/pinit.js" data-pin-build="parsePins"></script>
    

    Then you can render your buttons dynamically like so:

    // render buttons inside a scoped DOM element
    window.parsePins(buttonDomElement);
    
    // render the whole page
    window.parsePins();
    

    There is also another method on this site which lets you render them in JavaScript without the script tag.

    0 讨论(0)
  • 2021-01-30 18:45

    Just add data-pin-build attribute to the SCRIPT tag:

    <script defer
      src="//assets.pinterest.com/js/pinit.js"
      data-pin-build="parsePinBtns"></script>
    

    That causes pinit.js to expose its internal build function to the global window object as parsePinBtns function.

    Then, you can use it to parse links in the implicit element or all of the links on the page:

    // parse the whole page
    window.parsePinBtns();
    
    // parse links in #pin-it-buttons element only
    window.parsePinBtns(document.getElementById('pin-it-buttons'));
    

    Hint: to show zero count just add data-pin-zero="1" to SCRIPT tag.

    0 讨论(0)
  • 2021-01-30 18:48

    The best way to do this:

    1. Remove the iframe of the Pin It button you want to manipulate
    2. Append the html for the new button manipulating it as you wish
    3. Realod their script - i.e. using jQuery:

      $.ajax({ url: 'http://assets.pinterest.com/js/pinit.js', dataType: 'script', cache:true});
      
    0 讨论(0)
  • 2021-01-30 18:49

    I rewrote the Pinterest button code to support the parsing of Pinterest tags after loading AJAX content, similar to FB.XFBML.parse() or gapi.plusone.go(). As a bonus, an alternate JavaScript file in the project supports an HTML5-valid syntax.

    Check out the PinterestPlus project at GitHub.

    0 讨论(0)
  • 2021-01-30 18:49

    this works fine for me: http://www.mediadevelopment.no/projects/pinit/ It picks up all data on click event

    0 讨论(0)
  • 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 = '<a href="' + href + '" class="pin-it-button" count-layout="horizontal"><img border="0" src="http://assets.pinterest.com/images/PinExt.png" title="Pin It" /></a>';
            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;
    }
    
    0 讨论(0)
提交回复
热议问题