I followed this answer https://stackoverflow.com/a/7719185/842837 to load javascript asynchronously. How do I some pass data to this external file?
Current code:
<script id="js-widget-o2ba1y" type='text/javascript'>
(function() {
function async_load(){
var config = {
'module': 'hyperdesign',
'user-name': 'John Doe',
'user-email': 'john@doe.com'
};
var id = "js-widget-o2ba1y";
var embedder = document.getElementById(id);
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
var u = 'http://external.javascript.file.js';
s.src = u + ( u.indexOf("?") >= 0 ? "&" : "?") + 'ref=' + encodeURIComponent(window.location.href);
embedder.parentNode.insertBefore(s, embedder);
}
if (window.attachEvent)
window.attachEvent('onload', async_load);
else
window.addEventListener('load', async_load, false);
})();
</script>
How to I send config
to external js file?
EDIT I was going though google analytics code. They also load their script asynchronously, and use config data from 3rd party website:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
I need to do the same thing, in a more readable way.