The browser supports JavaScript and Cookies testing.

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; Session variables are used to record identity and other information, browser Cookies are required. Therefore, it is necessary to determine that these options in the user's browser are enabled. On my website, I use a simple string of code to implement these functions. When a user logs on, the system checks. If the user does not match the code, the system does not allow the user to log on. In this article, I will introduce how to write this login page.

<P style = line-height: 150%>
We first use JavaScript to create a Cookie and then check whether the Cookie exists. Because we use JavaScript to perform this operation, if the user does not open JavaScript but opens Cookies, we will still get the result that the Cookies are not opened. However, this does not conflict with our requirements. In any case, we need to enable both of them. (If you really only want to know whether Cookies are enabled, but do not care about JavaScript, there is a way to do it. We will discuss it in another article) Determine whether the user has enabled JavaScript, I created a hidden from in html, and then called a JavaScript function in the onload event to change the value of the hidden form. If the value is changed, it indicates that JavaScript is enabled, otherwise, this JavaScript function will not be called. (I wrote the above two functions in one function)

<P style = line-height: 150%> <! --Bkjia.com -->
First, we put a hidden form in html and enclose it with <form>... </form>. (Of course, there can also be username/password from in the middle)

<P style = line-height: 150%> <FORM>
...
<Input type = "hidden" name = "cookieexists" value = "false">
</FORM>

<P style = line-height: 150%> as long as its value is false, the browser does not support JavaScript. Note that the initial value is false. Our JavaScript function will replace this value with true. Write in the BODY as follows:
<Body onload = "cc ()">

<P style = line-height: 150%> 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! ");

<P style = line-height: 150%>/* If the user has Cookies are disabled an alert will let him know
That cookies need to be enabled to log on .*/

<P style = line-height: 150%> 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"
}
}

<P style = line-height: 150%>/* Set a cookie to be sure that one exists.
Note that this is outside the function */
Document. cookie = killme + escape (nothing)
// -->
</Script>

<P style = line-height: 150%>
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 execute the alert JavaScript statement to bring up a warning window, even if the prompt is not displayed), but after the user's from statement is submitted, the background program will find that the value of cookieexists is false, which means JavaScript is disabled. I don't need to talk about anything in the future?

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.