Determine whether the client supports JavaScript and Cookies

Source: Internet
Author: User

Many websites require the client to do a lot of complex work. For example, the client JavaScript is used for Data Validity verification, which requires the client's browser's
JavaScript enabled; using Session variables to record identity and other information requires browser Cookies enabled. Therefore, it is necessary to determine the user's browser
These options are enabled. On my website, I use a simple string of code to implement these functions. When a user logs on, check if the Code does not match.
Log on. In this article, I will introduce how to write this login page.

We first use JavaScript to create a Cookie and then check whether the Cookie exists. Because we use JavaScript to perform this operation, if
If the user does not open JavaScript, but opens Cookies, we will still get the result that the Cookies are not opened. But this is not in line with our requirements.
Suddenly, we need to open both of them. (If you really only want to know whether Cookies are enabled, but do not care about JavaScript, there is also a way
To determine whether the user opens JavaScript, I create a hidden from in html and then in the onload event
Call a JavaScript function to change the value of the hidden form. If the value is changed, JavaScript is enabled. Otherwise
JavaScript functions will not be called. (I wrote the above two functions in one function)

First, we put a hidden form in html and enclose it with <form>... </form>. (Of course, there can also be username/password
From)

<FORM>
...
<Input type = "hidden" name = "cookieexists" value = "false">
</FORM>

If its value is false, the browser does not support JavaScript. Note that the initial value is false. Our JavaScript function will change this value
True. Write in the BODY as follows:
<Body onload = "cc ()">

The content of cc () is as follows:
<Script language = "JavaScript">
<! -
Function cc ()
{
/* Check for a cookie */
If (document. cookie = "")
{
/* If a cookie is not found-alert user-
Change cookieexists field value to false */
Alert ("COOKIES need to be enabled! ");

/* If the user has Cookies are disabled an alert will let him know
That cookies need to be enabled to log on .*/

Document. Form1.cookieexists. value = "false"
} Else {
/* This sets the value to true and nothing else will happen,
The user will be able to log on */
Document. Form1.cookieexists. value = "true"
}
}

/* Set a cookie to be sure that one exists.
Note that this is outside the function */
Document. cookie = 'illme' + escape ('noth ')
// -->
</Script>


This program can implement the following functions:
1. When JavaScript is enabled and Cookies are disabled, a warning message is displayed.
2. When JavaScript is disabled, the user cannot directly obtain the check result. (Do not forget. To bring up a warning window, execute the alert command.
JavaScript statement, even if it is checked out, it cannot be prompted), but after the user's from is submitted, the background program will find cookieexists
The value of fields is false, which means JavaScript is disabled. I don't need to talk about anything in the future?

Translation: Challenge cat

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.