defer (HTML attribute)
Example
A script with
defer to stop the alert 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>
Description
The defer
attribute is little more than a hint that the browser should maybe,
possibly, if it feels like it, defer the execution of a script until later
on when it’s finished the job of downloading the page content, as the
script is not going to affect/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 being expressed in HTML 4.01. The XHTML syntax would be:
<script type="text/javascript" defer="defer"> … </script>
However, the only browser that supports it is also the one browser on the list that cannot properly handle an XHTML document when served with the correct MIME-type.
Value
"defer" only
Compatibility
| IE | 5.5 | Full |
|---|---|---|
| 6.0 | Full | |
| 7.0 | Full | |
| Firefox | 1.0 | None |
| 1.5 | None | |
| 2.0 | None | |
| Safari | 1.3 | None |
| 2.0 | None | |
| 3.0 | None | |
| Opera | 9.2 | None |
| 9.5 | None |
Very poor support -
only Internet Explorer honored the defer attribute,
all others ignored its presence. A better option is to use unobtrusive
JavaScript techniques that are called using a
window.onload event handler (or
similar).
User-contributed notes
There are no comments yet.
Add a note
To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

