Determine whether the client disables JavaScript and cookie

Source: Internet
Author: User
Generally, few users want to disable javascript. Therefore, JavaScript, Ajax, cookie, and other technologies are more or less used to achieve good user experience and webpage effects.

 

However, the existing Web attacks often use these mobile code (JavaScript, Cookie, Flash, Java applets, ActiveX, etc.) to attack clients for illegal purposes. Therefore, some users with high security awareness use the functions provided by the browser to disable or shield JS and cookie.

Therefore, it is necessary to determine whether the client Javascript is disabled and prompt the user to enable the JavaScript function. See the following code:

Java code
  1. <Div id = "nojs"> you disabled JavaScript. </Div>
  2. <Div id = "yesjs"> body </div>
  3. <SCRIPT>
  4. VaR nojs = Document. getelementbyid ("nojs ");
  5. Nojs. classname = "close ";
  6. // Close the style code. Close {display: none ;}
  7. </SCRIPT>
<Div id = "nojs"> you disabled JavaScript. </Div> <Div id = "yesjs"> body </div> <SCRIPT> var nojs = document. getelementbyid ("nojs"); nojs. classname = "close"; // close the style code. close {display: None ;}</SCRIPT>

 

The principle of this code is very simple, that is, if the javascrpt code is executed, the DIV with the ID of nojs is hidden, otherwise it is displayed.

 

The following code uses JavaScript to determine whether the cookie function is Enabled:

JS Code
  1. <SCRIPT>
  2. Function cookieenable (){
  3. VaR result = false;
  4. If (navigator. cookiesenabled) return true;
  5. Document. Cookie = "testcookie = yes ;";
  6. VaR cookieset = Document. Cookie;
  7. If (cookieset. indexof ("testcookie = yes")>-1) Result = true;
  8. Document. Cookie = "";
  9. Return result;
  10. }
  11. If (! Cookieenable ()){
  12. Alert ("sorry, your browser's cookie function is disabled, Please enable ");
  13. }
  14. </SCRIPT>
<SCRIPT> function cookieenable () {var result = false; If (navigator. cookiesenabled) return true; document. cookie = "testcookie = yes;"; var cookieset = document. cookie; If (cookieset. indexof ("testcookie = yes")>-1) Result = true; document. cookie = ""; return result;} If (! Cookieenable () {alert ("sorry, your browser's cookie function is disabled. Please enable") ;}</SCRIPT>

The above method is not the only solution or the best solution. It depends on the actual situation. In short, I think we should improve the availability and ease-of-use of the website. We should start by disabling JS, cookies, and so on, so that cookies and JS cannot affect the normal functions of the website.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.