Angrycreative / bj-lazy-load

WordPress plugin for lazy loading
GNU General Public License v2.0
40 stars 25 forks source link

Embeds with <script src> aren't lazy lazy loaded #19

Open dimadin opened 8 years ago

dimadin commented 8 years ago

Some embed providers use <iframe> for loading of their content, but other use plain HTML or iframe followed by <script src>. Most notable example is Twitter, but there are others like Tumblr, Vine, Instagram...

Here is a Twitter example:

<blockquote class="twitter-tweet" width="500"><p lang="en" dir="ltr">Some content...</p>&mdash; Milan Dinić (@dinicmilan) <a href="https://twitter.com/dinicmilan/status/682154806358720512">December 30, 2015</a></blockquote><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

This means that their JavaScript file is loaded and <iframe> created while loading page and lazy loading is not working for them. In my commit dimadin/bj-lazy-load@5bf81af8ca418890a3b130f4de8c86df40f45ae3, I showed how this can be done for Twitter, but this is not practical because all sources must be manually defined and they can change.

What can be done is to do some regex to find script source, then replace that <script> tag with placeholder image, while JavaScript can stay the same or similar as in my commit. The only question is what to do if script has more attributes.

Other option is to put some trigger in your JavaScript file so that I don't need to edit that file after updates.