[英]Error “Element script must not have attribute defer unless attribute src is also specified” from W3C validator
I have an HTML document whose source has a script
element that has a defer
attribute.我有一个 HTML 文档,其源代码有一个带有
defer
属性的script
元素。 The W3C validator says it isn't valid because it lacks the src
attribute, but in fact there is a src
attribute is in the source: W3C 验证器说它无效,因为它缺少
src
属性,但实际上源中有一个src
属性:
<script defer type="text/javascript">if($(window).width()>1024){document.write("<"+"script src='js/jquery.preloader.js'></"+"script>");}</script>
What can I do?我能做什么?
Maintainer of the W3C HTML Checker (validator) here. W3C HTML Checker(验证器)的维护者在这里。
The fix is to put the script contents into a separate file and use the src
attribute to reference that.解决方法是将脚本内容放入一个单独的文件中,并使用
src
属性来引用它。
The checker reports an error for the markup snippet in the question because the HTML spec used to explicitly state that the script
can't have a defer
attribute unless it also has a src
attribute.检查器报告问题中标记片段的错误,因为 HTML 规范用于明确声明
script
不能具有defer
属性,除非它也具有src
属性。
The spec no longer seems to explicitly state that restriction, but I think that's just an unintentional regression caused by a later spec change (which your question helped to catch—so, thanks!).规范似乎不再明确说明该限制,但我认为这只是由后来的规范更改引起的无意回归(您的问题有助于捕捉到 - 所以,谢谢!)。
So I'll investigate and raise a pull request for the spec fix and then update this answer later.因此,我将调查并提出针对规范修复的拉取请求,然后稍后更新此答案。
2018-05-05 update 2018-05-05 更新
For the record here, I did end up filing a pull request with an HTML spec patch:作为记录,我最终提交了一个带有 HTML 规范补丁的拉取请求:
https://github.com/whatwg/html/pull/2509 https://github.com/whatwg/html/pull/2509
…and that was merged into the HTML spec source: ……然后合并到 HTML 规范源代码中:
https://github.com/whatwg/html/commit/3c5180a08f90a375c64f8191f32f8c7ddfec0ba3 https://github.com/whatwg/html/commit/3c5180a08f90a375c64f8191f32f8c7ddfec0ba3
…and so now the current HTML spec once again states the restriction: ……所以现在当前的 HTML 规范再次声明了限制:
https://html.spec.whatwg.org/multipage/scripting.html#attr-script-defer https://html.spec.whatwg.org/multipage/scripting.html#attr-script-defer
The
async
anddefer
attributes are boolean attributes that indicate how the script should be evaluated.async
和defer
属性是布尔属性,指示应如何评估脚本。 Classic scripts may specifydefer
orasync
, but must not specify either unless thesrc
attribute is present.经典脚本可以指定
defer
或async
,但除非存在src
属性,否则不得指定。
You have two <script>
tags.你有两个
<script>
标签。
document.write
.document.write
生成的。 The HTML one looks like this: HTML 看起来像这样:
<script defer type="text/javascript">…</script>
It has no src
attribute.它没有
src
属性。 That is why you get the error.这就是你得到错误的原因。 You can't have a
defer
attribute there.那里不能有
defer
属性。
The one generated by JavaScript looks like this: JavaScript 生成的代码如下所示:
if ($(window).width()>1024){
document.write("<"+"script src='js/jquery.preloader.js'></"+"script>");
}
It has a src
attribute and doesn't have a defer
attribute.它有一个
src
属性,没有一个defer
属性。 You could add a defer
attribute here.您可以在此处添加
defer
属性。
It is also JavaScript so won't be treated as HTML by the validator anyway.它也是 JavaScript,因此无论如何都不会被验证器视为 HTML。
Try to set your DOCTYPE to HTML5.尝试将您的 DOCTYPE 设置为 HTML5。 If not feasable, you'll probably need to write
defer=true
instead of defer
.如果不可行,您可能需要编写
defer=true
而不是defer
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.