Instagram embeds not working when adding embeds dynamically

m4olivei picture m4olivei · Dec 10, 2014 · Viewed 14.8k times · Source

Working on a blog that loads posts on an infinite scroller. Each blog post may or may not have Instagram embeds. I'm finding that the first one that shows on the page will get processed (regardless if it's in the initial page markup, or dynamically added), the following ones will not. Here is a simple JS Bin that illustrates the problem:

http://jsbin.com/hilixi/1/edit?html,js,output

The first Instagram embed is in the initial page markup. Another Instagram embed is added after page load, after 4 seconds. The second embed add does not get processed.

Any ideas why? It seems the Instagram embed script will only run once. Any way I can force it to refresh?

Thanks, Matt

Answer

narwic picture narwic · Dec 10, 2014

The embed.js script that comes with Instagram's embed code has a process function you can call.

window.instgrm.Embeds.process()

Just use that whenever your dynamic content loads.