defer (HTML attribute)
| Depr. | Version |
|---|---|
| No | HTML 4 |
| IE5.5+ | FF2 | SA4 | OP10 | CH2 |
|---|---|---|---|---|
| Full | None | None | None | None |
Syntax
Description
The
defer attribute is little more than a hint that the
browser should—possibly, if it feels like it—defer the execution of a
script until later, once it’s finished the job of downloading the page’s
DOM content, as the script isn’t going to affect or modify the content of
the page in any way, shape, or form.
In the example above, Internet
Explorer honors the defer attribute and correctly
identifies the button’s name.
The example shows the attribute expressed in HTML 4.01 syntax. The XHTML syntax would be as follows:
<script type="text/javascript" defer="defer"> ⋮ </script>
However, the only browser that supports this is also the one browser on the list that can’t properly handle an XHTML document that’s served with the correct MIME type.
Example
This example shows a
script element with the defer
attribute set in order to prevent the alert from taking place too early
(at which point the button being referred to does not yet
exist):
<script type="text/javascript" defer> alert(document.forms[0].cmdButton.value); </script> ⋮ <form> <input type="submit" name="cmdButton" value="Send it"> </form>
Value
The only value that
defer can take is
"defer".
Compatibility
| Internet Explorer | Firefox | Safari | Opera | Chrome | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 5.5 | 6.0 | 7.0 | 8.0 | 1.0 | 1.5 | 2.0 | 1.3 | 2.0 | 3.1 | 4.0 | 9.2 | 9.5 | 10.0 | 2.0 |
| Full | Full | Full | Full | None | None | None | None | None | None | None | None | None | None | None |
Very poor support is
provided for this attribute. Only Internet Explorer honors
defer; all other browsers ignore its presence. A
better option is to use unobtrusive JavaScript techniques that are called
using the window.onload event handler or a JavaScript
library that runs when the document is read. For example, in jQuery the
code within the function below will run when the document is
ready:
$(document).ready(function(){
//run functions here
})
.