Script Tags With Ids?
Solution 1:
HTML 4 Answer:
No, you can't, at least not if you want valid HTML...
The following elements can't have an ID attribute:
<base>
<head>
<html>
<meta>
<script>
<style>
<title>
There might be a couple more.
This is confusing because viewing just the documentation for the ID/class attributes doesn't specifically say that they can't be used with these elements. You have to look at the DTD for the elements to see that the general attributes are not defined for the element, and thus cannot be used.
HTML 5 Answer:
The default document type declaration, HTMLElement, which applies to all elements in HTML specifies that these global attributes (including the ID attribute) can be used on any element you create, so it would appear you can do this in HTML 5, but not in HTML 4.
Solution 2:
Running a <script>
tag with an id
through the validator as HTML4 generates this error:
Error Line 12, Column 12: there is no attribute "ID"
The HTML4 specification for <script>
does not mention the coreattrs (that include class
and id
) to be valid on this element:
18.2.1 The SCRIPT element
<!ELEMENT SCRIPT - - %Script; -- scriptstatements --><!ATTLIST SCRIPTcharset %Charset; #IMPLIED -- charencodingoflinkedresource --
type %ContentType; #REQUIRED -- contenttypeofscriptlanguage --
src %URI; #IMPLIED -- URIforanexternalscript --
defer (defer) #IMPLIED -- UAmaydeferexecutionofscript --
>
Start tag: required, End tag: required
According to the current HTML5 spec, <script>
may have any Global Attributes which does include id
. Testing this in an unofficial HTML5 validator passes. So as far as I can tell:
- Invalid in HTML4
- Valid in HTML5
Post a Comment for "Script Tags With Ids?"