Reputation: 10372
Do most modern browsers support ids in script tags such as:
<script id="aParticularScript">/* ... */</script>
The reason I ask is that Eclipse displays a warning stating "undefined attribute name" but it works fine in Google Chrome when I use jQuery selectors to get other properties of the script element. W3Schools states that the script element does not support any standard attributes (including the id attribute), but I've learned not to trust W3Schools.
Is it okay to have the script tag have an id?
Upvotes: 4
Views: 5791
Reputation: 102735
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; -- script statements -->
<!ATTLIST SCRIPT
charset %Charset; #IMPLIED -- char encoding of linked resource --
type %ContentType; #REQUIRED -- content type of script language --
src %URI; #IMPLIED -- URI for an external script --
defer (defer) #IMPLIED -- UA may defer execution of script --
>
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:
Upvotes: 2
Reputation: 54719
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.
Upvotes: 6