Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instagram embeds not working when adding embeds dynamically

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

like image 998
m4olivei Avatar asked Dec 10 '14 19:12

m4olivei


People also ask

How do I enable Instagram embed?

Instagram.com from a mobile browser: Tap your profile picture in the bottom right to go to your profile. Tap Settings in the top right. Tap Privacy and Security. Below Embeds, Tap to check the box next to Allow people to embed your posts or profile on other websites to turn the setting on.

Is Instagram embedding legal?

Unless you have been advised by a lawyer that fair use truly applies, you should continue to avoid embedding without permission. Photographers have filed multiple lawsuits against publishers for embedding social media posts of images (see, e.g., Sinclar v. Ziff Davis, McGucken v. Newsweek).

What is embeded in Instagram?

Embed codes allow your profile or content to be shared along with your username and a link to your Instagram profile. Note that for a post or profile to be embedded, your account must be public and the Embeds setting must be turned on. Private accounts and content from private accounts cannot be embedded.


2 Answers

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.

like image 185
cy23 Avatar answered Oct 23 '22 23:10

cy23


In your app.js create a directive for adding script element:

.directive('externalscript', function() {
   var injectScript = function(element) {
    var instagramScript = angular.element(document.createElement('script'));
    instagramScript.attr('async defer');
    instagramScript.attr('charset', 'utf-8');
    instagramScript.attr('src', 'http://platform.instagram.com/en_US/embeds.js');
    instagramScript.attr('onload', 'window.instgrm.Embeds.process()');
    element.append(instagramScript);
    var twitterScript = angular.element(document.createElement('script'));
    twitterScript.attr('async defer');
    twitterScript.attr('charset', 'utf-8');
    twitterScript.attr('src', 'http://platform.twitter.com/widgets.js');
    element.append(twitterScript);
};

  return {
      link: function(scope, element) {
          injectScript(element);
      }
  };
})

and then in your html view call:

<div externalscript></div>
like image 42
Darko Kostic Avatar answered Oct 23 '22 22:10

Darko Kostic