Determine whether the client has JavaScript and Cookie functions

Source: Internet
Author: User
On my site, I want to confirm whether visitors have cookies and JavaScript functions. Maintaining the session status is complex in many cases, and form verification is also required. On my site, I used the client JavaScript for form verification, so to complete the verification, the user's browser must have the JavaScript function. At the same time, I also use the Se Cookie

On my site, I want to confirm whether visitors have cookies and JavaScript functions. Maintaining the session status is complex in many cases, and form verification is also required. On my site, I used the client JavaScript for form verification, so to complete the verification, the user's browser must have the JavaScript function. At the same time, I also used the Session variable, which requires the visitor's browser to support the Cookies function. To confirm the existence of these prerequisites, I used a small piece of code to check whether users have JavaScript and Cookies. If the user does not meet these two conditions, they are rejected to log on to the website. This article also introduces the logon page code of my website.

Create a Cookie using JavaScript. Then, check whether the cookie exists. Because we use JavaScript to test whether users enable Cookies. if they do not have JavaScript but have Cookies, we will get the results that they do not have Cookies. This result is good because I require users to have both JavaScript and Cookies. To determine whether a user has the JavaScript function, I created a hidden form field on the HTML page and called the event (onload) on the page) to change the value of the hidden form field. In this way, if the value of the form field changes, JavaScript will take effect.

First, place a hidden domain on the HTML page and place it between <form>... </form>.



As long as the value of this field is false, we know that the user's browser does not support JavaScript. Note that the default value is false, and the JavaScript function we compile will change it to true. Therefore, if JavaScript takes effect, the JavaScript function runs and the value of the field changes to true. In the Body tag of the HTML page, place the code that calls the JavaScript function to check cookies (cc ):

<Body onload = "cc ()">

If the function is not executed (this happens only when JavaScript is invalid), the cc () function will not change the value of the hidden field. The cc () function code is quite simple, as follows:

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.