javascript 检查Javascript是否启用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6683594/
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
Check whether Javascript is enabled
提问by Jake
Is there a way to check whether Javascript is enabled or supported by the browser? If it's not supported, I would like to redirect the user to a user-friendly error page.
有没有办法检查浏览器是否启用或支持Javascript?如果不支持,我想将用户重定向到用户友好的错误页面。
I am using jQuery and the PHP Zend Framework.
我正在使用 jQuery 和 PHP Zend 框架。
回答by Mike Samuel
<noscript><meta http-equiv="refresh" content="1;url=error.html"></noscript>
This will redirect to an error page if script is disabled. Just replace error.html
with the URL of your error page.
如果脚本被禁用,这将重定向到错误页面。只需替换error.html
为您的错误页面的 URL。
回答by Brad Christie
As yet another option, you can (though it requires a second page visit) use javascript to set a cookie.
作为另一种选择,您可以(尽管它需要第二次访问页面)使用 javascript 来设置 cookie。
If the cookie exists server-side (they have javascript) render the page as normal. During the absense of the cookie, you can either use a Location
redirect, or render the appropriate [stripped-down] template to accommodate their lack of javascript support.
如果 cookie 存在于服务器端(他们有 javascript),则正常呈现页面。在 cookieLocation
缺失期间,您可以使用重定向,或呈现适当的 [精简] 模板以适应它们缺乏 javascript 支持。
page html
页面 html
<script type="text/javascript">
document.cookie = 'hasJS=true';
</script>
page php
页面 php
if (isset($_COOKIE['hasJS'])){
// normal page render
}else{
header('Location: http://mysite.com/index-nojs.php');
}
回答by digitalbath
Whether or not javascript is enabled is only known from within the context of a browser, so the best you can do is write some browser-side code to set a flag based on if javascript is enabled or not. One possibility is do something like
是否启用 javascript 只能在浏览器的上下文中知道,因此您能做的最好的事情是编写一些浏览器端代码来根据是否启用 javascript 设置一个标志。一种可能性是做类似的事情
<noscript><img src="/javascript_disabled.php"></noscript>
and
和
// contents of javascript_disabled.php
$_SESSION['javascript_disabled'] = 1;
回答by marc
As the default, send out the version without javascript. There you include a little piece of javascript that redirects to the dynamic version. This will only get executed when js is enabled.
默认情况下,发送不带 javascript 的版本。在那里你包含了一小段重定向到动态版本的 javascript。这只会在启用 js 时执行。
回答by naivists
You can make a simple "landing page" for users without javascript AND add a javascript redirection to the javascript-enabled version of site.
您可以为没有 javascript 的用户制作一个简单的“登陆页面”,并将 javascript 重定向添加到启用了 javascript 的网站版本。
Something like this:
像这样的东西:
...
<html>
...
<script type="text/javascript">
window.location.replace("/hasjs");
</script>
回答by DMichael
Piece of cake!
小菜一碟!
Encompass your entire Javascript page in one DIV.
将整个 Javascript 页面包含在一个 DIV 中。
Overlay a second DIV of equal size that contains your non-Javascript page. Give that DIV an id and a high z-index:
覆盖包含非 Javascript 页面的相同大小的第二个 DIV。给那个 DIV 一个 id 和一个高 z-index:
div id="hidejs" style="z-index: 200"
In this second non-JS DIV, have your very first code be Javascript that sets this DIV's visibility to hidden:
在第二个非 JS DIV 中,让您的第一个代码是 Javascript,将这个 DIV 的可见性设置为隐藏:
document.getElementById.("hidejs").style.visibility = "hidden";
No Javascript enabled? Nothing will happen and they'll see the overlying non-Javascript page.
没有启用 Javascript?什么都不会发生,他们会看到覆盖的非 Javascript 页面。
Javascript enabled? The overlying non-Javascript page will be made invisible and they'll see the underlying Javascript page.
启用Javascript?覆盖的非 Javascript 页面将不可见,他们将看到底层的 Javascript 页面。
This also lets you keep the whole page in one file, making modifications easier, rather than trying to manage two files for the same page.
这也让您可以将整个页面保存在一个文件中,使修改更容易,而不是尝试为同一页面管理两个文件。
回答by Brian Graham
Use <noscript>
tags to include a meta-redirect if the page does not have JavaScript.
<noscript>
如果页面没有 JavaScript,请使用标签来包含元重定向。
<noscript>
tags are called when the browser connecting to the page does not have JavaScript.
<noscript>
当连接到页面的浏览器没有 JavaScript 时会调用标签。
回答by Localghost
Bring the user to the error page by default, and have a javascript redirect execute in the section of the page. Have the redirect push the user to the real page.
默认情况下将用户带到错误页面,并在页面部分执行 javascript 重定向。让重定向将用户推送到真实页面。
If they don't have javascript enabled, they won't get redirected and the rest of the page (error page) will load.
如果他们没有启用 javascript,他们将不会被重定向,页面的其余部分(错误页面)将加载。
回答by adswebwork
Yes. Make you have the latest jQuery.
是的。让你拥有最新的jQuery。
Javascript:
Javascript:
$(function(){ $('#jsEnabled2').html('Yes it is') })
PHP:
PHP:
$js - 'No';
$jscheck = 'Javascript Enabled: ';
$jscheck .= '<span id="jsEnabled">'.$js.'</span>';
print $jscheck;