2

For example, google+ gives this code:

<!-- Place this tag after the last +1 button tag. -->
<script type="text/javascript">
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
</script>

but why not just this:

<script async src="https://apis.google.com/js/plusone.js"></script>
1

2 Answers 2

3

async and its cousin defer aren't supported by all browsers. (IE didn't get them until IE10, for instance.) The first method will always be async, the second won't be on browsers that don't support async.

The reason the first will always be async is that script elements added to the DOM dynamically like that don't hold up the parsing and rendering of the document, whereas <script> tags inline in the HTML content do (barring the use — and support — of async or defer).

Sign up to request clarification or add additional context in comments.

2 Comments

According to this, Opera is the only modern browser that doesn't support it.
@Whymarrh: Depends on your definition of "modern." IE8 will be with us a long, long time. (Amusingly, I was just adding a link to caniuse to the answer as you commented!)
0

There are good explanation: Which browsers support <script async="async" />?

Yes, now most browsers support async but usually you need 100% guaranty that you code works, and then you need to use first form

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.