最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Defering javascript - what is the correct html syntax defer or defer="defer" - Stack Overflow

programmeradmin0浏览0评论

What is the correct syntax for using the defer attribute in your javascript?

I have seen it done two ways:

1:

<script defer  >...</script>

2:

<script defer="defer">...</script>

From experience [and a reference I cannot find] I am more inclined to used the second option, but I just double checked the official w3c site and it seems that option 1 is correct.

Thanks

What is the correct syntax for using the defer attribute in your javascript?

I have seen it done two ways:

1:

<script defer  >...</script>

2:

<script defer="defer">...</script>

From experience [and a reference I cannot find] I am more inclined to used the second option, but I just double checked the official w3c site and it seems that option 1 is correct.

Thanks

Share Improve this question asked Jun 18, 2013 at 12:40 Dai BokDai Bok 3,6063 gold badges57 silver badges70 bronze badges 2
  • 1 Second variant seems like a XHTML – z1m.in Commented Jun 18, 2013 at 12:42
  • Thanks for the quick answers! Going for 2nd option to "future-proof" the control – Dai Bok Commented Jun 18, 2013 at 14:36
Add a comment  | 

4 Answers 4

Reset to default 11

defer is a boolean attribute [HTML 4.01 spec]:

Some attributes play the role of boolean variables (e.g., the selected attribute for the OPTION element). Their appearance in the start tag of an element implies that the value of the attribute is "true". Their absence implies a value of "false".

[...]

In HTML, boolean attributes may appear in minimized form -- the attribute's value appears alone in the element's start tag. Thus, selected may be set by writing:

<OPTION selected>

instead of:

<OPTION selected="selected">

Authors should be aware that many user agents only recognize the minimized form of boolean attributes and not the full form.

However, if you use XHTML, you have to use the second form since XHTML follows XML syntax where attributes always have to have a value.

Since you reference HTML 4.01:

This is a Boolean Attribute. Both forms are correct, but the specification recommends the former.


If you were using XHTML then you would have to use the longer version.

HTML 5 also allows both versions and removes the recommendation to use one over the other (since for compatibility with XHTML served as text/html, all modern browsers can handle both syntaxes).

HTML 5.1 nightly

2.4.2 Boolean attributes

A number of attributes are boolean attributes. The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

so defer is right, and so is defer="defer" and defer="DeFeR" and defer=""

The async and defer attributes are boolean attributes that indicate how the script should be executed. The defer and async attributes must not be specified if the src attribute is not present.

https://www.w3.org/TR/html5/scripting-1.html#attr-script-defer

发布评论

评论列表(0)

  1. 暂无评论