如果禁用javsascript,请使用服务器端代码

时间:2022-06-01 17:04:13

i'm delegating some application logic client side (javascipt). How can i switch to server side only if javascript is disabled.

我正在委派一些应用程序逻辑客户端(javascipt)。如果禁用了javascript,我怎样才能切换到服务器端。

eg

例如

if(javascript.isdisabled)
{
    //execute server code
}

3 个解决方案

#1


3  

You do it the other way around. You write HTML that works with server side code, then layer JavaScript over the top that stops the HTTP request that would trigger the server side code.

你反过来做。您编写的HTML与服务器端代码一起使用,然后将JavaScript层叠在顶部,以阻止将触发服务器端代码的HTTP请求。

The specifics depend on exactly what you want to do, but will usually involve calling the Event.preventDefault() method having bound an event listener.

具体取决于您想要做什么,但通常涉及调用绑定事件监听器的Event.preventDefault()方法。

For example, given a form:

例如,给定一个表格:

function calc(evt) {
    var form = this;
    alert(+this.elements.first.value + +this.elements.second.value);
    evt.preventDefault()
}

var form = document.querySelector('form');
form.addEventListener('submit', calc);

See also: Progressive Enhancement and Unobtrusive JavaScript

另请参阅:渐进增强和不引人注目的JavaScript

#2


0  

Server code executes first, then is sent client side. And there's no good way to determine server side whether JS is turned on.

服务器代码首先执行,然后发送到客户端。并且没有好的方法来确定服务器端是否打开了JS。

So purely: no. You simply don't know if JS is enabled until the point where the server already done serving that request.

如此纯粹:没有。您只是不知道JS是否已启用,直到服务器已完成为该请求提供服务为止。

Solutions are as follows:

解决方案如下:


1) Do it manually (not recommended)

1)手动(不推荐)

As long as this is not happening on the user's first page view, you could determine on the first page view whether or not JS is enabled, and then tell all future requests to the server that information manually. One way to accomplish that would be to have all links have a query var telling the server to execute logic, but after the page loads remove that var via JS (which obviously will only happen if there is JS).

只要在用户的第一个页面视图中没有发生这种情况,您就可以在第一个页面上查看是否启用了JS,然后手动将所有未来的请求告诉服务器。实现这一目标的一种方法是让所有链接都有一个查询var告诉服务器执行逻辑,但在页面加载后通过JS删除该var(显然只有在JS时才会发生)。

So a link would look like https://blah.com/my-page?serverexecute=1 in the page, then once the page loads JS (if it's enabled) can remove the var so it's just https://blah.com/my-page. Then the server would only executed your logic if the query var serverexecute is present and set to 1.

所以一个链接看起来像页面中的https://blah.com/my-page?serverexecute=1,然后一旦页面加载JS(如果它已启用)可以删除var所以它只是https://blah.com /我的页面。然后,如果查询var serverexecute存在并且设置为1,则服务器将仅执行您的逻辑。

But this would be very non-standard and, frankly, weird. The more normal way to do this is:

但这非常不标准,坦率地说,很奇怪。更常见的方法是:


2) Reverse your thinking (recommended)

2)扭转你的想法(推荐)

As said in another answer: progressive enhancement. This is the norm. You serve a page with the expectation that no other scripting be needed (i.e. do what has to be done server side) and then use JS as enhancement on top of that only.

如在另一个答案中所述:渐进增强。这是常态。您需要提供一个页面,期望不需要其他脚本(即执行必须在服务器端完成的操作),然后仅使用JS作为增强功能。


3) Don't cater to non-JS (also recommended, personally anyway)

3)不要迎合非JS(也推荐,个人无论如何)

JS availability is an insanely high percentage. It is considered a norm, and you'd be surprised how many sites don't actually work without it.

JS的可用性是一个非常高的百分比。它被认为是一种规范,如果没有它,有多少网站实际上无法工作,你会感到惊讶。

Note that I'm not saying "just let it break silently", but rather show a message at the top (or wherever is relevant) saying that the site or part of the site may not function correctly without JS (this can be done via noscript tags quite easily).

请注意,我不是说“只是让它静静地破解”,而是在顶部(或相关的任何地方)显示一条消息,说如果没有JS,网站或部分网站可能无法正常运行(这可以通过noscript标签非常容易)。

A notable example of this is none other than Facebook. I just tried going to facebook with JS disabled. I wasn't logged in to anything, so I got to the signup page, and above the form it noted:

一个值得注意的例子就是Facebook。我刚刚尝试使用JS禁用facebook。我没有登录任何东西,所以我进入了注册页面,并在表格上方注明:

"JavaScript is disabled on your browser. Please enable JavaScript on your browser or upgrade to a JavaScript-capable browser to register for Facebook."

“您的浏览器禁用了JavaScript。请在您的浏览器上启用JavaScript或升级到支持JavaScript的浏览器以注册Facebook。”

They didn't even make the effort to stop the form from showing...just, in essence, told me "by the way, it won't work".

他们甚至没有努力阻止表格显示......只是,实质上,告诉我“顺便说一下,它不会起作用”。

Unless there's some very specific requirement that means you absolutely need non-JS (beyond the normal general "let's be accessible" concept) I personally believe there is currently absolutely no reason to spend any effort catering to non-JS users beyond the courtesy of a noscript letting them know that you're not catering to them.

除非有一些非常具体的要求意味着你绝对需要非JS(超出正常的一般“让我们可以访问”的概念),我个人认为,除了礼貌之外,目前绝对没有理由花费任何努力来迎合非JS用户。 noscript让他们知道你不会迎合他们。

#3


-1  

You could think of redirecting users without javascript to a special page, that includes server-side logic you've mentioned, like so:

您可以考虑将没有javascript的用户重定向到特殊页面,其中包括您提到的服务器端逻辑,如下所示:

<head>
  <noscript>
    <meta http-equiv="refresh" content="0; url=http://example.com/without-js" />
  </noscript>
</head>

The page may be exactly same page featuring query string that will tell server to perform the logic you've mentioned.

该页面可能与查询字符串完全相同,该字符串将告诉服务器执行您提到的逻辑。

Another possible approach to consider is explained in "Detect if JavaScript is enabled in ASPX" article.

另一种可能的考虑方法在“检测是否在ASPX中启用JavaScript”一文中进行了解释。

#1


3  

You do it the other way around. You write HTML that works with server side code, then layer JavaScript over the top that stops the HTTP request that would trigger the server side code.

你反过来做。您编写的HTML与服务器端代码一起使用,然后将JavaScript层叠在顶部,以阻止将触发服务器端代码的HTTP请求。

The specifics depend on exactly what you want to do, but will usually involve calling the Event.preventDefault() method having bound an event listener.

具体取决于您想要做什么,但通常涉及调用绑定事件监听器的Event.preventDefault()方法。

For example, given a form:

例如,给定一个表格:

function calc(evt) {
    var form = this;
    alert(+this.elements.first.value + +this.elements.second.value);
    evt.preventDefault()
}

var form = document.querySelector('form');
form.addEventListener('submit', calc);

See also: Progressive Enhancement and Unobtrusive JavaScript

另请参阅:渐进增强和不引人注目的JavaScript

#2


0  

Server code executes first, then is sent client side. And there's no good way to determine server side whether JS is turned on.

服务器代码首先执行,然后发送到客户端。并且没有好的方法来确定服务器端是否打开了JS。

So purely: no. You simply don't know if JS is enabled until the point where the server already done serving that request.

如此纯粹:没有。您只是不知道JS是否已启用,直到服务器已完成为该请求提供服务为止。

Solutions are as follows:

解决方案如下:


1) Do it manually (not recommended)

1)手动(不推荐)

As long as this is not happening on the user's first page view, you could determine on the first page view whether or not JS is enabled, and then tell all future requests to the server that information manually. One way to accomplish that would be to have all links have a query var telling the server to execute logic, but after the page loads remove that var via JS (which obviously will only happen if there is JS).

只要在用户的第一个页面视图中没有发生这种情况,您就可以在第一个页面上查看是否启用了JS,然后手动将所有未来的请求告诉服务器。实现这一目标的一种方法是让所有链接都有一个查询var告诉服务器执行逻辑,但在页面加载后通过JS删除该var(显然只有在JS时才会发生)。

So a link would look like https://blah.com/my-page?serverexecute=1 in the page, then once the page loads JS (if it's enabled) can remove the var so it's just https://blah.com/my-page. Then the server would only executed your logic if the query var serverexecute is present and set to 1.

所以一个链接看起来像页面中的https://blah.com/my-page?serverexecute=1,然后一旦页面加载JS(如果它已启用)可以删除var所以它只是https://blah.com /我的页面。然后,如果查询var serverexecute存在并且设置为1,则服务器将仅执行您的逻辑。

But this would be very non-standard and, frankly, weird. The more normal way to do this is:

但这非常不标准,坦率地说,很奇怪。更常见的方法是:


2) Reverse your thinking (recommended)

2)扭转你的想法(推荐)

As said in another answer: progressive enhancement. This is the norm. You serve a page with the expectation that no other scripting be needed (i.e. do what has to be done server side) and then use JS as enhancement on top of that only.

如在另一个答案中所述:渐进增强。这是常态。您需要提供一个页面,期望不需要其他脚本(即执行必须在服务器端完成的操作),然后仅使用JS作为增强功能。


3) Don't cater to non-JS (also recommended, personally anyway)

3)不要迎合非JS(也推荐,个人无论如何)

JS availability is an insanely high percentage. It is considered a norm, and you'd be surprised how many sites don't actually work without it.

JS的可用性是一个非常高的百分比。它被认为是一种规范,如果没有它,有多少网站实际上无法工作,你会感到惊讶。

Note that I'm not saying "just let it break silently", but rather show a message at the top (or wherever is relevant) saying that the site or part of the site may not function correctly without JS (this can be done via noscript tags quite easily).

请注意,我不是说“只是让它静静地破解”,而是在顶部(或相关的任何地方)显示一条消息,说如果没有JS,网站或部分网站可能无法正常运行(这可以通过noscript标签非常容易)。

A notable example of this is none other than Facebook. I just tried going to facebook with JS disabled. I wasn't logged in to anything, so I got to the signup page, and above the form it noted:

一个值得注意的例子就是Facebook。我刚刚尝试使用JS禁用facebook。我没有登录任何东西,所以我进入了注册页面,并在表格上方注明:

"JavaScript is disabled on your browser. Please enable JavaScript on your browser or upgrade to a JavaScript-capable browser to register for Facebook."

“您的浏览器禁用了JavaScript。请在您的浏览器上启用JavaScript或升级到支持JavaScript的浏览器以注册Facebook。”

They didn't even make the effort to stop the form from showing...just, in essence, told me "by the way, it won't work".

他们甚至没有努力阻止表格显示......只是,实质上,告诉我“顺便说一下,它不会起作用”。

Unless there's some very specific requirement that means you absolutely need non-JS (beyond the normal general "let's be accessible" concept) I personally believe there is currently absolutely no reason to spend any effort catering to non-JS users beyond the courtesy of a noscript letting them know that you're not catering to them.

除非有一些非常具体的要求意味着你绝对需要非JS(超出正常的一般“让我们可以访问”的概念),我个人认为,除了礼貌之外,目前绝对没有理由花费任何努力来迎合非JS用户。 noscript让他们知道你不会迎合他们。

#3


-1  

You could think of redirecting users without javascript to a special page, that includes server-side logic you've mentioned, like so:

您可以考虑将没有javascript的用户重定向到特殊页面,其中包括您提到的服务器端逻辑,如下所示:

<head>
  <noscript>
    <meta http-equiv="refresh" content="0; url=http://example.com/without-js" />
  </noscript>
</head>

The page may be exactly same page featuring query string that will tell server to perform the logic you've mentioned.

该页面可能与查询字符串完全相同,该字符串将告诉服务器执行您提到的逻辑。

Another possible approach to consider is explained in "Detect if JavaScript is enabled in ASPX" article.

另一种可能的考虑方法在“检测是否在ASPX中启用JavaScript”一文中进行了解释。