Home > php教程 > PHP开发 > body text

Determine whether the client has JavaScript and Cookie functions

高洛峰
Release: 2016-11-25 09:48:23
Original
1299 people have browsed it

On my site, I want to confirm whether my visitors have cookies and JavaScript enabled. Maintaining session state is complex in many cases, and also has to take form validation into account. On my site, I use client-side JavaScript for form validation, so to complete the validation, the user's browser must have JavaScript capabilities. At the same time, I also used Session variables, 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 the user has JavaScript and Cookies capabilities. If the user does not meet these two conditions, they will be denied access to the website. In this article, I also introduce the login page code of my site. To get started, use JavaScript to create a cookie. Then, check whether the cookie exists. Because we use JavaScript to test whether the user has turned on the Cookies function, if they do not have JavaScript but have Cookies, we will get the result that they do not have the Cookies function. This result is great because I require the user to have both JavaScript and Cookies capabilities. In order to determine whether the user has JavaScript capabilities, I created a hidden form field on the HTML page, and called a JavaScript function in the page call event (onload) to change the value of the hidden form field. In this way, if the value of the form field changes, it means that JavaScript is effective. 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, the JavaScript function we wrote will change it to true. So, if JavaScript is enabled, 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 only happens when JavaScript is not in effect) , the cc() function will not change the value of the hidden field. The code of the cc() function is quite simple, as follows: Note: The JavaScript code lines outside the function will be executed before the page is loaded, so they are called before the cc() function. Normally, JavaScript code blocks should be placed in the HEAD section of the HTML page. In this way, if JavaScript takes effect but Cookies does not take effect, the user will get a message that "the cookie function needs to be turned on". If JavaScript does not take effect, no information will be displayed. In this case, we cannot determine whether the cookies function is turned on. When the form is submitted, check the value of the hidden field cookieexists. If equal to false, it means JavaScript is not supported. Please indicate the source when reprinting: Determine whether the client has JavaScript and Cookie functions


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!