仅在禁用 JavaScript 时显示“启用 JavaScript”消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5334329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display "Enable JavaScript" message only when JavaScript is disabled
提问by Michael
I want to display to the user a message that says "please enable JavaScript" in the case that JavaScript is disabled. I want that message, and nothing else to be displayed when JavaScript is disabled.
我想在 JavaScript 被禁用的情况下向用户显示一条消息,上面写着“请启用 JavaScript”。我想要该消息,并且在禁用 JavaScript 时不显示任何其他内容。
So, to do this, I can put the message in the DOM and hide all other elements with display:none. Then in JavaScript I can set the message to display hidden and show all other elements.
因此,为此,我可以将消息放入 DOM 并使用 display:none 隐藏所有其他元素。然后在 JavaScript 中,我可以将消息设置为隐藏并显示所有其他元素。
But I get a flicker with this approach. The error message shows up for a while (especially on mobile browsers) before it gets hidden.
但是我对这种方法感到困惑。错误消息在隐藏之前会显示一段时间(尤其是在移动浏览器上)。
How can I minimize the time the error message is displayed for?
如何最大限度地减少显示错误消息的时间?
回答by SLaks
You're looking for the <noscript>
tag.
您正在寻找<noscript>
标签。
回答by Mansour
Inline javascript has to run before the rest of the document is loaded. You can use this behaviour to add style to the page to hide the desired element, beforethe element is loaded. This should theoretically stop FOUC across all and every browser (including mobile). Here's an example of a standalone HTML that shows a message to those with no javascript. This technique also takes care of what Hussein was mentioning about Firewalls blocking javascript:
内联 javascript 必须在加载文档的其余部分之前运行。在加载元素之前,您可以使用此行为向页面添加样式以隐藏所需元素。从理论上讲,这应该会阻止所有浏览器(包括移动设备)上的 FOUC。这是一个独立 HTML 的示例,它向没有 javascript 的人显示消息。这种技术还处理了侯赛因提到的关于防火墙阻止 javascript 的内容:
<!doctype html>
<html>
<head>
<title>No FOUC</title>
<script type="text/javascript">
document.write('<style type="text/css">#no-js { display: none; }</style>');
</script>
</head>
<body>
<div id="no-js">You don't have javascript, BOOOO</div>
</body>
</html>
回答by Hussein
You can do this. If JavaScript is disabled it will show the message "JavaScript Disabled"
你可以这样做。如果 JavaScript 被禁用,它将显示消息“JavaScript Disabled”
<p id="jsDisabled">JavaScript disabled.</p>
<script type="text/javascript">
function hideScriptDisabled() {
document.getElementById("jsDisabled").display = "none";
}
hideScriptDisabled();
</script>
If that is creating a flickering problem, use something like this,
如果这造成了闪烁问题,请使用类似的方法,
document.write(".jsDisabled { display: none; }");
回答by K232
回答by user3738184
To avoid javascript disable problem.
为了避免javascript禁用问题。
before starting any writing on your webpage just put the below code.
在您的网页上开始任何写作之前,只需输入以下代码。
<!-- saved from url=(0014)about:internet-->
<!-- saved from url=(0014)about:internet-->
You have to never ask any question to end user.
您永远不必向最终用户提出任何问题。
First try it.
先试试吧。
回答by DA.
You really can't. The flicker is just the way it is...especially on slow mobile devices (namely Nokia...OH HOW I HATE NOKIA!)
你真的不能。闪烁就是它的样子......尤其是在慢速移动设备上(即诺基亚......哦,我多么讨厌诺基亚!)
The only other option is to load a splash page first. In the HEAD add a meta refresh of several seconds that will refresh to a 'turn on javascript error'. On the BODY, add a javascript redirect that should trigger immediately.
唯一的其他选择是首先加载启动页面。在 HEAD 中添加几秒钟的元刷新,这将刷新为“打开 javascript 错误”。在 BODY 上,添加应立即触发的 javascript 重定向。
You will still have a flash, and of course, one more page request to the server. But it may be a 'prettier' flash.
您仍然会有一个闪现,当然,还有一个对服务器的页面请求。但它可能是一个“更漂亮”的闪光。