Dynamically and asynchronously loading numerous LinkedIn share buttons on a page

On my page, I have a grid of post thumbnails that are fetched via AJAX and can be filtered. When a user clicks on a thumbnail, a carousel opens with the selected post centered. In this carousel, each post has a LinkedIn share button integrated.

The issue arises when trying to load the LinkedIn share buttons into an invisible element initially and then making it visible later. This causes the buttons not to work properly. To solve this problem, I use the code snippet below:

$.getScript('http://platform.linkedin.com/in.js', function() { 
  $('.li-box-1').append('<script type="IN/Share" data-counter="right"></script>');
});

However, if I close the carousel, filter the posts, select a different one, and open the carousel again, the LinkedIn share button is missing. Additionally, a warning appears in the console stating:

duplicate in.js loaded, any parameters will be ignored

This warning occurs because LinkedIn's in.js script has already been loaded previously. Does anyone have a solution for this issue?

Answer №1

Below is the snippet of code that will include the mandatory linked in .js library. We first check if the library has been loaded before by verifying if the variable IN is undefined. Depending on this condition, we either load the library for the first time or skip the process.
This piece of code should be placed within your <header> section, following the <body> tag, or just before the closing </body>, depending on your specific case.

<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>  

Alternatively, you can also utilize the following method:

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

Now, place this code alongside your custom carousel or carousel items.

<script type="IN/Share" 
        data-url=" **code to generate your url** " 
        data-counter="right">
</script>

Answer №2

Upon examining the script being executed, one can observe that the output of the .getScript function is not directly injected into the script tag itself; rather, two distinct actions are taking place: loading the script and then generating the tag with type="IN/Share". The initial step of loading the script only needs to be performed once, as previously noted, hence it suffices to execute the .append command to create any desired dynamic buttons and subsequently invoke IN.parse() to establish a connection between the script and the freshly generated elements.

Answer №3

It seems like you're utilizing some advanced coding techniques just to share a link on LinkedIn. Have you considered trying a simpler method?

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

This way, you'll have the flexibility to hyperlink anything you desire, customize it with CSS and JS, and more.

Source: Microsoft LinkedIn Share URL Documentation.

Here's an example that works for me:

It functions perfectly:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

byte sequence displays as binary data (angular + express)

I've been working on pulling files from a back-end Node.js / Express server and displaying them in an Angular front-end. Despite trying different methods, I keep facing the same issue - the data displayed at the front-end appears as a bytestring. Even ...

Exploring the world of data manipulation in AngularJS

Seeking to comprehend the rationale behind it, I will share some general code snippets: 1) Fetching data from a JSON file using the "loadData" service: return { myData: function(){ return $http.get(path + "data.json"); } } 2) ...

Redux accesses the store data after a brief delay

I am new to using redux and I am currently working on implementing it for my very first project. Everything seems to be working fine, however, when I try to console.log the data from the redux store, initially it shows two empty arrays but in the subsequen ...

What is the method for obtaining a unique exception message in the ajaxError event of jQuery?

Is there a way to customize the content of responseText in my APP? Currently, it displays as exception description amended by Tomcat (refer to the image below). I want to have control over the content of responseText or handle custom messages in a more eff ...

What are the steps to integrate and utilize DefinePlugin within your webpack configuration?

Hello, I'm currently trying to implement the DefinePlugin in order to update the version number and ensure that my JavaScript refreshes after a new build is released. Unfortunately, I am encountering issues with getting DefinePlugin to work properly. ...

Unusual marking on the navigation bar

Currently, I am making updates to a website that was created by a previous employee long before I joined the team. One of the requested changes is to eliminate the orange box surrounding the navigation links. The navigation appears to be generated using Ja ...

Suggestions for relocating this function call from my HTML code

I have been working on updating a javascript function that currently uses an inline event handler to a more modern approach. My goal is to eliminate the unattractive event handler from the actual HTML code and instead place it in a separate modular javascr ...

Transfer the controller of the parent directive to a directive controller, similar to how it is executed in the "link" function

In the structure of my directives, there is a need for one directive to have functions in its controller so that child elements can interact with it. However, this directive also needs access to its parent directive's controller. I am unsure of how to ...

What could be the reason for the sudden lack of content from the Blogger API?

For weeks, I've been using the Google API to retrieve JSON data from my Blogger account and showcase and style blog posts on my personal website. Everything was functioning flawlessly until yesterday when, out of the blue, the content section stopped ...

Persistent change issue with JQuery CSS function

Looking for some help with a button-bar div that I want to display only after the user clicks on a "settings" button. Currently, I have set the button-bar div to have a display:none property in my css file. However, when the settings button is clicked, t ...

Node.js encountered an error due to a self-signed certificate within the certificate chain

I'm encountering an issue with client-side HTTPS requests. An example snippet is as follows: var fs = require('fs'); var https = require('https'); var options = { hostname: 'example.com', port: 443, path: & ...

In which location can one find the compiled TypeScript files within an Angular 2 project?

As a newcomer to learning Angular 2, I've come across tutorials that mention all compiled files should go into the dist folder. These compiled files refer to typescript files transpiled into JavaScript. However, upon creating my project using Angular ...

Issue with utilizing display: table and overflow: hidden in Internet Explorer and Firefox, functioning properly on Webkit and Blink

JSFiddle : http://jsfiddle.net/h7xvr2t9/2/ I have been experimenting with ways to achieve some effects: Animating a hidden DIV to slide into view from below a visible container Centering text/image on a link to trigger the animation HTML <div clas ...

Tips for manipulating the DOM: Pushing existing elements when new ones are dynamically created with JavaScript

I have a pre-existing DOM element (let's say a div) and I am using JavaScript, specifically React, to create a new element - another div which serves as a sidebar. The goal is for the sidebar to seamlessly shift the previous element without overlappin ...

What is the best way to convert my JSON data to HTML using JavaScript?

My dilemma seems to be rooted in the use of backticks. Strangely, an error keeps popping up whenever I try to employ them in my IDE (Brackets). I attempted solutions by testing directly in my Browser and through NotePad++, but unfortunately, nothing seeme ...

I am unable to correctly fetch the data string using Jquery Ajax from the server

I have implemented a jQuery Ajax function to check the availability of a username in real-time from the database. If the username is not available, the response is marked as "Unavailable" and vice versa. While I am successfully receiving this response, I a ...

What is the best way to iterate through a designated key in every object within a JSON dataset and store it in a variable called `data` for use with

I am looking to create a line chart displaying stock prices, utilizing JSON data to populate the chart. Let's say I have some JSON data: [ { "close": 116.59, "high": 117.49, "low": 116.22, "open& ...

Trouble with Loading Google Place Autocomplete API in HTML

I utilized the Google MAP Place Autocomplete API for a web project. Click here to view the code and output. <form> <input id="origin-input" type="text" class="form-control" placeholder="From"/> <br/> <input id="de ...

Issue: Module 'xml2json' not found

Encountered an error while running my project package. When I tried to install the necessary packages using npm install xml2json I still encountered another error below. Can anyone provide suggestions or ideas on how to resolve this issue? D:\xa ...

What is the procedure to change a matter body's isStatic property to false in matter.js upon pressing a key?

During my recent project, I encountered a challenge in trying to set the isStatic property of a Matter Body in matter.js to false when a key is pressed. if (keyIsPressed(UP_ARROW)) { this.body.isStatic(false) } Could you provide guidance on the correct ...