HTML中的空脚本标记是怎么回事? [英] What's the thing with empty script tags in HTML?

查看:60
本文介绍了HTML中的空脚本标记是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在HTML页面中包含外部JavaScript文件,您将使用以下代码:

To include an external JavaScript file in HTML page you would use this:

<script type="text/javascript" src="images/jquery-2.2.2.js"></script>

此标记的主体为空,但我必须两次键入字符串 script .为什么在HTML中没有XML样式的隐式结束标记,像这样:

The body of this tag is empty, yet I have to type the string script twice. Why isn't XML-style implicit closing tag in HTML, like this:

<script type="text/javascript" src="images/jquery-2.2.2.js" />

如果它是有效的(至少在HTML5中有效),那么如何确保每个支持HTML5(或XHTML5)的浏览器都可以使用这种用法?

If it is valid (at least in HTML5), then how to make sure every browser supporting HTML5 (or XHTML5) can pick up on this usage?

我将把这个问题带给HTML WG.弃用< script> 上的 src 属性,因为它是整个混乱的根源,它具有以下两个选项之一:

I'm gonna take this question to HTML WG. Deprecate the src attribute on <script> as it is the source of this entire confusion, with one of the two options:

  • < link> 标记,而不是空的< script> 标记:

  • <link> tag instead of an empty <script> tag:

<link type="text/javascript" href="images/jquery-2.2.2.js" />

或(可选)具有 rel 属性:

<link rel="script" type="text/javascript" href="images/jquery-2.2.2.js" />

JavaScript本身中的

  • PHP样式的 include():(因此始终将 some 内容强制放入标记中,并允许在其中包含多个脚本一个 script 标记)

  • PHP-style include() in JavaScript itself: (thus force some content into the tag at all times, and allow multiple scripts to be included in one script tag)

    <script type="text/javascript">include("images/jquery-2.2.2.js");</script>
    

  • 推荐答案

    根据规范,不允许 省略HTML5中< script> 的结束标记(请参见标记省略"部分):

    Omitting the closing tag for <script> in HTML5 is not permissible, according to the specs (see the Tag Omission sections):

    https://developer.mozilla.org/zh-美国/docs/Web/HTML/Element/script

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

    https://html.spec.whatwg.org/multipage/scripting.html#the-script-element

    这篇关于HTML中的空脚本标记是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆