Determine if the client has JavaScript and cookie functionality _php
Source: Internet
Author: User
Keywordsfunction with client whether judge JavaScript
Cookies
On my site, I want to make sure that visitors have cookies and JavaScript capabilities. Maintaining session state is complex in many cases, and also takes into account validation of forms. On my site, I used client-side JavaScript for form validation, so the user's browser must have JavaScript functionality to complete the validation work. At the same time, I used the session variable, which requires the visitor's browser to support the cookie function. To confirm the existence of these prerequisites, I used a small piece of code to check whether the user has JavaScript and cookie functionality. If the user does 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 users turn on cookies, if they do not have JavaScript, but they have cookies, we will get the result that they do not have the function of cookies. This result is very good because I require the user to have both JavaScript and cookie functionality. 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 this hidden form field. This way, if the numeric 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 is in effect, this JavaScript function will run, and the value of the domain will become true. In the body tag of the HTML page, place the code that invokes the JavaScript function to check for cookies (cc):
< body onload= "CC ()" >
The CC () function does not change the value of the hidden field if the function does not execute (this happens only when JavaScript does not take effect). The code for the CC () function is quite simple, as follows:
Note: The JavaScript code line outside the function executes before the page is paged in, so it is called before the CC () function. Typically, JavaScript blocks of code should be placed in the head section of an HTML page.
This way, if JavaScript is in effect and the cookie does not take effect, the user will be prompted with a "need to open cookies" message. If JavaScript does not take effect, no information will be displayed, in which case we cannot determine if the cookie function is turned on. When the form is submitted, check the value of the hidden field cookieexists. If equal to False, then 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.