3 ms·
For something unrelated, what's the point of code like this? <script> (function() { var hn = document.createElement('script'); hn.type =
by nilved 15y ago
For something unrelated, what's the point of code like this?
<script>
(function() {
var hn = document.createElement('script'); hn.type = 'text/javascript';
hn.async = true; hn.src = 'http://hnbutton.appspot.com/static/hn.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(hn, s);
})();
</script>
Why not just put in the generated script tag? Moreover, why does it matter where the tag is placed if it's loaded with async as opposed to defer?
- igrigorik 15y agoFor the HN audience you probably could, since we're all on modern browsers. The pattern above is what GA uses to guarantee most consistent behavior across all browsers. It comes down to browser support for the async keyword: http://stackoverflow.com/questions/1834077/which-browsers-support-script-async-async/6766189#6766189 http://stackoverflow.com/questions/1834077/which-browsers-su... As far as async vs. defer, here's a good summary: http://www.sitepoint.com/non-blocking-async-defer/ http://www.sitepoint.com/non-blocking-async-defer/