我需要检查浏览器JavaScript是否已关闭,然后显示错误div而不是正文,我该怎么做?
答案 0 :(得分:7)
你需要反过来做 - 可以这么说 - 你必须输出所有内容,然后使用Javascript隐藏/删除错误div。
它被称为渐进增强。
答案 1 :(得分:6)
<html class="no-js">
<head>
<style>
.error,
.no-js #container {
display: none;
}
.no-js .error {
display: block;
}
</style>
<script>
document.documentElement.className = document.documentElement.className.replace(/\bno-js\b/, '');
</script>
</head>
<body>
<div id="container">
rest of page
</div>
<div class="error">
sorry, no javascripty, no sitey!
</div>
</body>
</html>
当然,这通常是一个坏主意,但我希望你已经考虑过了。
答案 2 :(得分:4)
:
<noscript>
Here the html to display when javascript is off
</noscript>
答案 3 :(得分:1)
以下内容将在正文渲染之前移除html.no-js
类:
<!DOCTYPE html>
<html class="no-js">
<head>
<script>
if (document.documentElement) {
var cn = document.documentElement.className;
document.documentElement.className = cn.replace(/no-js/,'');
}
</script>
</head>