OpenShare / openshare

Customizable API wrappers for social platforms
http://openshare.social
113 stars 17 forks source link

Doesn't work in Async Scripts #5

Closed imsus closed 8 years ago

imsus commented 8 years ago

Using data-attributes and initiate with async scripts. Demo

Because no console error logs. It's hard to track.

jakeburden commented 8 years ago

I'm so glad you've reported this 👍

This bug is really interesting. I've found the problem:

We have been using document.addEventListener('DOMContentLoaded', callback) to ensure the data-attributes are on the page before openshare runs. However, with <script async ... openshare is actually being parsed after the DOM loads, which means openshare is missing the DOMContentLoaded event and never runs.

What we ought to do instead (or additionally) is check for document.readyState. This is what jQuery(document).ready() does. Or, alternatively, use document.onreadystatechange.

If you want to take on the initiative to send a pull request I'd be glad to see it through and answer any questions along the way!

Otherwise, I'll try my best to fix this soon.

Thank you @imsus 🎉

himanshu81494 commented 8 years ago

I would like to work on it.

jakeburden commented 8 years ago

Merged the fix from @matteo-hertel and published it to npm

@imsus you should be able use the cdn reference in this demo until the cache on cdn.rawgit.com refreshes

thank you so much everyone 😄