Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script tags with IDs?

Tags:

html

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?

like image 460
Ivan Avatar asked Feb 22 '23 23:02

Ivan


1 Answers

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.

like image 186
animuson Avatar answered Mar 07 '23 07:03

animuson