Assigning a script id to a tag

I came across a script in which the script attribute id attribute could easily solve the problem. However, after reading the script element in w3schools and quirksmode , it looks like this may have some unforeseen consequences.

Does anyone encounter any of these issues with browsers like Chrome, Safari, FF3, and IE 7?

+48
html standards scripting
Apr 29 '10 at 23:40
source share
5 answers

This is great in all modern browsers.

The only browser that received the <script id> was Netscape 4, which we stopped caring about a long time ago.

This quirksmode page looks badly outdated, which with its use of the language , script <!-- attributes <!-- hidden by application/x-javascript . His advice on avoiding <script> in <body> (and putting it in <head> ) is not consistent with today's encouraged practices.

If we are talking about compatibility issues with <script> attributes: defer doesn't work everywhere, so don't rely on it; charset does not work everywhere, and neither the charset parameter on the served script is Content-Type, so your script encoding does not fit the page better; type should always be text/javascript , and not one of the non-working alternatives that pedants who wrote RFC 4329 would like to use.

+72
Apr 29 '10 at 23:51 on
source share

If you still need to support Netscape 4, you have many problems - both pity and condolences to the rest of the developer worlds.

The short answer is, I would not worry about that.

+6
Apr 29 '10 at 23:52
source share

I don't think the browser will have a problem adding the id attribute to the script element.

On some of my sites that download additional JavaScripts via JavaScript, I have added a class attribute to simplify their link. The validator did not complain.

+2
Apr 29 '10 at 23:51 on
source share

I know that a lot of time has passed, but I thought it would be nice to indicate that when you look at the definition of the W3 Schools script tag , at some point you see that

The tag also supports Global Attributes in HTML .

and among these ta-da attributes you will find your beautiful identifier.

The same can be said for the many tags that will undoubtedly give us great flexibility when you pull this beautiful trick out of your hat.

+2
Nov 09 '16 at 9:43
source share

Keep in mind that setting id for any element represents a new global variable with the same name as the id attribute:

id as a global variable

+1
Feb 07 '17 at 22:29
source share



All Articles