Skip to: content, navigation

defer (HTML attribute)

Spec
Depr. Version
No HTML 4
Browser support (more…)
IE5.5+ FF2 SA3 OP9.5
Full None None None

Syntax

defer="defer"

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
5.5 6.0 7.0 1.0 1.5 2.0 1.3 2.0 3.0 9.2 9.5
Full Full Full 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 a window.onload event handler (or similar scripting).

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.

Related Products

Search