baynezy
baynezy

Reputation: 7066

<script defer> not working as expected

I am trying to defer my script execution because it is not important that it executes before the page is rendered. However, it is not working as I understand it.

According to this article (and others like it), if I use defer then the execution order is preserved. However, when using highlight.js I am seeing a different behaviour. My script files look like so:-

<script defer type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.7/highlight.min.js"></script>
<script defer type="text/javascript">hljs.initHighlightingOnLoad();</script>

Now as I understand it they should execute in order which is unlike the behaviour of async. However, I get the following error in Chrome (version 44.0.2403.157).

If I remove the defer attributes it works as expected. What am I not understanding correctly?

Upvotes: 3

Views: 3906

Answers (1)

Margus
Margus

Reputation: 20058

The defer attribute has been browser specific http://caniuse.com/#feat=script-defer (thanks to atmd)

The defer and async attributes must not be specified if the src attribute is not present.

Check your second case.

Upvotes: 6

Related Questions