noscript alternative in XHTML?

北慕城南 提交于 2019-12-29 08:40:11

问题


In HTML I could do:

<noscript><link href="css/stylenojs.css" rel="stylesheet" type="text/css" /></noscript>

Is there some compliant way to do so in XHTML Transitional or Strict documents?


回答1:


The example you give is invalid in HTML as well as XHTML. No current recommendation provides a way to include a stylesheet unless scripting is enabled.

In general, you should avoid <noscript>. Start with something that works, and then build on it.

In this case, you could write your stylesheet for non-JS clients, then:

<body>
<script type="text/javascript">document.body.className += ' js';</script>

… and include additional rule-sets specific to JS being enabled with:

body.js foo {
}

Alternatively, you could do something like:

<link href="css/stylenojs.css" rel="stylesheet" type="text/css" id="nojscss" />

and

var nojscss = document.getElementById('nojscss');
nojscss.parentNode.removeChild(nojscss);



回答2:


The current chosen answer is misleading.

<noscript> is HTML valid as well as the example provided in the question.

W3C says:

The noscript element is only effective in the HTML syntax, it has no effect in the XHTML syntax. This is because the way it works is by essentially "turning off" the parser when scripts are enabled, so that the contents of the element are treated as pure text and not as real elements. XML does not define a mechanism by which to do this.

Now, the little dirty secret about XHTML is that browsers are not treating XHTML in XML unless the documented is served in application/xhtml+xml as the MIME type.

In case your page is served as proper XHTML, then a solution might be changing the doctype of the page to HTML5 and MIME type to text/html.



来源:https://stackoverflow.com/questions/4902658/noscript-alternative-in-xhtml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!