Loading mulitple LinkedIn share buttons dynamically and asynchronously

后端 未结 3 855
遥遥无期
遥遥无期 2021-01-19 19:35

I have a page showing thumbnails of posts. The posts are fetched via AJAX and a filter allows for different posts to be fetched. When a thumbnail is clicked, a carousel open

相关标签:
3条回答
  • 2021-01-19 20:30

    This is the code that calls in the required linked in .js library. We check to see if the library has been loaded previously by checking if the variable IN is undefined. And based on that we load the library for the first time, or ignore it.
    This code you will put somewhere in your <header> tag, after the <body> tag, or right before the </body>, dont know your situation.

    <script>
    if (typeof (IN) !== 'undefined') {
      // IN.parse(); // old but still supports
      IN.init();  // reinitiating linkedin button  
    } else {
      $.getScript("http://platform.linkedin.com/in.js");
    }   
    </script>  
    

    or alternatively you could do this:

    <script>
      delete IN;
      $.getScript("http://platform.linkedin.com/in.js")
    </script>
    

    And now this code you will place with your specific carousel, or carousel items.

    <script type="IN/Share" 
            data-url=" **code to generate your url** " 
            data-counter="right">
    </script>
    
    0 讨论(0)
  • 2021-01-19 20:30

    If you look at the script you're running, you'll see that the results of the .getScript isn't being loaded into the script tag or anything like that, but rather you're essentially performing two seperate actions: loading the script and then creating the tag with type="IN/Share". The initial action, loading the script, only needs to be run once, as you've discovered, so you just need to run that .append line to create whatever dynamic buttons you want and then call IN.parse() to link the script to the newly created elements.

    0 讨论(0)
  • 2021-01-19 20:37

    Seems like you're doing some really amazing coding gymnastics just to be able to share a link on LinkedIn. Why not try something simpler?

    https://www.linkedin.com/sharing/share-offsite/?url={url}
    

    Then you can hyperlink anything you want, control it with whatever CSS and JS you want, etc..

    Source: Microsoft LinkedIn Share URL Documentation.

    For example, this works for me:

    https://www.linkedin.com/sharing/share-offsite/?url=http://www.wikipedia.org/

    Works fine:

    0 讨论(0)
提交回复
热议问题