如果禁用javsascript,請使用服務器端代碼

[英]Use server side code if javsascript is disabled


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”一文中進行了解釋。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/06/03/71298cbbd702c68cab2c34cfb79410f6.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com