How to declare JavaScript and CSS XHTML compliant?

I am wondering how to declare JavaScript code in a CDATA section so that it is XHTML compatible. Which method is the correct / recommended?

Method 1:

<script type="text/javascript"> // <![CDATA[ CODE // ]]> </script> 

Method 2:

 <script type="text/javascript"> /* <![CDATA[ */ CODE /* ]]> */ </script> 

Is the second suitable for inline CSS?

And is it possible / it makes sense to add some encoding declaration here, for example

 <script type="text/javascript" charset="utf-8"> ... <style type="text/css" media="screen" charset="utf-8"> ... or <script type="text/javascript"> @charset "utf-8"; ... <style type="text/css" media="screen"> @charset "utf-8"; ... 
+4
source share
3 answers

Which method is the correct / recommended?

Both are equally good.

Is the second suitable for inline CSS?

Yes.

However, you only need to worry about placing the script and style blocks in the <![CDATA[ section if you want to include the < or & characters in the block. You will almost never use them in CSS.

You can use them in JavaScript, but as a rule, it is better to avoid problems by placing any significant part of the code in external scripts where it belongs. Inline script blocks tend to be better for placing data and script-invocation calls, which rarely need to use < or & (except for the values โ€‹โ€‹of a string literal, in which case there is an argument for encoding them, for example, \x3C ).

And is it possible / meaningful to add some encoding declaration here

Not. The charset="..." attribute only makes sense for external files (and does not exist on <style> , since it is never an external file). The internal content has already been decoded from bytes to characters at the same time as the rest of the containing document, so charset does not make sense at this point.

In any case, not all browsers pay attention to the <script charset> attribute, so you should make the encoding of the scripts correspond to the encoding of the page that includes them. Which makes charset="..." pretty redundant.

@charset "utf-8"; OK in external style sheets, but you almost never need non-ASCII characters in style sheets, so it's unusual to see it. @charset not valid in JavaScript.

+6
source

It depends on how you maintain your XHTML.

If you use XHTML as XHTML, use the following HTTP header ...

 Content-Type:application/xhtml+xml 

... then you only need a CDATA section with a commented beginning and end; just like you.

However, if you use HTML ...

 Content-Type:text/html 

Everything is getting a little more complicated. You should read the article Submitting XHTML as text / html, which is considered harmful , and also form your own opinion on this issue; whether or not XHTML is actually used as text/html is indeed often discussed; for example, Elliote Rusty Harold (author of HTML Refactoring , which I highly recommend) disagrees.

+1
source

I use this and never get in trouble:

 <script type="text/javascript" charset="utf-8"> ... </script <style type="text/css" media="screen" charset="utf-8"> ... </style> 
-2
source

Source: https://habr.com/ru/post/1310536/


All Articles