javascript javascript关闭时如何处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3802086/
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
How to handle when javascript is turned off
提问by Mark Estrada
Possible Duplicate:
How to detect if JavaScript is disabled?
可能的重复:
如何检测 JavaScript 是否被禁用?
My first web application relies heavily on Javascript and AJAX application.
我的第一个 Web 应用程序严重依赖于 Javascript 和 AJAX 应用程序。
Other than my Login Page, majority of the pages relies on Javascript and Ajax when submitting data to the server. I have been thinking about this, if user disables his/her javascript then my app does not behave accordingly.
除了我的登录页面,大多数页面在向服务器提交数据时都依赖于 Javascript 和 Ajax。我一直在考虑这个问题,如果用户禁用了他/她的 javascript,那么我的应用程序不会相应地运行。
I used this code in my Login page
我在登录页面中使用了此代码
<NOSCRIPT>
Javascript is required to run this pages. Please turn it on or ask help from techsupport if you dont know how to enable it
</NOSCRIPT>
Although I think not relevant, I used Spring MVC 2.5 and Jquery for the sake of information to all.
虽然我认为不相关,但为了给大家提供信息,我使用了 Spring MVC 2.5 和 Jquery。
Any thoughts how others are able to handle in scenario such as this?
任何想法其他人如何能够在这样的情况下处理?
回答by Blixt
I think it's fine to require JavaScript to run your application. All mainstream web browsers have it today, and have had it for the last 10 years.
我认为要求 JavaScript 来运行您的应用程序是可以的。今天所有主流 Web 浏览器都拥有它,并且在过去 10 年里一直拥有它。
Using a <noscript>tag is an acceptable way of showing to the user that they need JavaScript. I don't understand what you meant about disabling the login button though... You used JavaScript to detect if JavaScript was disabled ("I used jquery to check for the presence of this tag")?
使用<noscript>标签是向用户表明他们需要 JavaScript 的一种可接受的方式。我不明白你说禁用登录按钮是什么意思……你用 JavaScript 来检测 JavaScript 是否被禁用(“我用 jquery 来检查这个标签的存在”)?
What you should do is to have the login button disabled by default, then use JavaScript (jQuery) to enable it. That way it will only be enabled if the user has JavaScript enabled.
您应该做的是默认禁用登录按钮,然后使用 JavaScript (jQuery) 启用它。这样,只有在用户启用了 JavaScript 时才会启用它。
回答by RobertPitt
What you should do is something like so
你应该做的是这样的
<noscript>
<meta http-equiv="refresh" content="url=/?nojs=true">
</noscript>
This will do a html immediate refresh as soon as the headers are parsed allowing you to do something a little different.
这将在解析标题后立即进行 html 刷新,让您可以做一些不同的事情。
Server Side: (PHP in my case)
服务器端:(在我的例子中是 PHP)
if(isset($_GET['nojs']) && !isset($_SESSION['no_script']))
{
$_SESSION['no_script'] = true; //Or cookie
}
Then as you have the variable locked in session / cookie you can throw out different pages for non JavaScript users.
然后,当您将变量锁定在会话/cookie 中时,您可以为非 JavaScript 用户丢弃不同的页面。
回答by tpodmani
5! If you web applications heavily relies on JavaScript my suggestion is to include <noscript>tag. Then you may inform users how to enable it: www.enable-javascript.com
5!如果您的 Web 应用程序严重依赖 JavaScript,我的建议是包含<noscript>标签。然后你可以通知用户如何启用它:www.enable-javascript.com
回答by Alexander Sagen
If your application is unable to work without javascript, which is fair, I think you're approach is perfectly reasonable. Let them know and keep them from entering your site until js is running. That's way better than having your app fail miserably.
如果您的应用程序在没有 javascript 的情况下无法运行,这是公平的,我认为您的方法是完全合理的。让他们知道并阻止他们进入您的网站,直到 js 运行。这比让您的应用程序惨遭失败要好得多。
That being said, if it is possible to make your app work without javascript, it's a judgement call whether it's worth the effort.
话虽如此,如果有可能让您的应用程序在没有 javascript 的情况下工作,这是一个判断是否值得付出努力的判断。
回答by Reigel
I used jquery to check for the presence of this tag and then disable the Login Button.
我使用 jquery 检查此标记的存在,然后禁用登录按钮。
I think it's better if you disabled the login button if it requires heavily javascript. Then just do $('#login').attr('disabled',false);. When javascript is not disabled, this will enable the login button.
如果登录按钮需要大量 javascript,我认为最好禁用它。然后就做$('#login').attr('disabled',false);。当 javascript 没有被禁用时,这将启用登录按钮。
And also, add some divs telling nice message, styled with css, then just remove it when JS is turned on.
而且,添加一些 div 来告诉好消息,用 css 样式,然后在打开 JS 时将其删除。
But I should warn you to also check it server-sideto make sure. because if I use firebug, I can enable the button easy as 1,2,3. To check server side, maybe do .ajax()calls for session or cookie. Then act accordingly. When someone logs in, check that variable.
但是我应该警告您还要检查服务器端以确保。因为如果我使用萤火虫,我可以像 1、2、3 一样轻松启用按钮。要检查服务器端,可以.ajax()调用 session 或 cookie。然后采取相应的行动。当有人登录时,检查该变量。

