cookie| client on my site, I want to make sure that visitors have cookies and JavaScript features. Maintaining session state is complex in many cases, and it also takes into account validation of the form. On my site, I use client JavaScript for form validation, so the user's browser must have JavaScript capabilities to do the validation work. At the same time, I also used the session variable, which requires the visitor's browser to support cookies function. To confirm the existence of these prerequisites, I used a small piece of code to check whether the user has JavaScript and cookies. If users do not have these 2 conditions, they are denied access to the site. In this article, I also introduce the login page code for my site.
Start by using JavaScript to create a cookie. Then, check to see if the cookie exists. Because we use JavaScript to test whether the user has the ability to turn on cookies, if they do not have JavaScript, but they have cookies, we will get the result that they do not have cookies. This result is good because I ask the user to have JavaScript and cookies at the same time. To determine if the user has JavaScript capabilities, I created a hidden form field on the HTML page and called a JavaScript function in the page invoke event (onload) to change the value of the hidden form field. This way, if the value of the form field changes, it means that JavaScript is in effect.
First, place a hidden field on the HTML page and place it between < form > ... </form >.
As long as the value of this field is false, then we know that the user's browser does not support JavaScript. Note that the default value is False, and the JavaScript function we write will change it to true. So, if JavaScript takes effect, this JavaScript function will run and the value of the field will become 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 does not execute (this happens only when JavaScript does not work), the CC () function does not change the value of the hidden field. The code for the CC () function is fairly simple, 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 disabled an alert'll let him know
That cookie need to is enabled to log on.*/
Document. Form1.cookieexists.value = "false"
} else {
/* This sets the value of to true and nothing else would happen,
Note: JavaScript lines outside the function are executed before the page is transferred, so that is called before the CC () function. Typically, JavaScript code blocks should be placed in the head section of an HTML page.
In this way, if JavaScript takes effect and the cookies do not take effect, the user will be prompted with the "need to turn on cookies function" message. If JavaScript does not work, no information will be displayed, in which case we cannot judge whether the cookies feature is turned on. When the form is submitted, check the value of the hidden field cookieexists. If equal to False, JavaScript is not supported.
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.