Home php教程 php手册 判断客户端是否具备javascript和Cookie功能

判断客户端是否具备javascript和Cookie功能

Jun 21, 2016 am 09:14 AM
cookie cookies javascript quot

cookie|客户端

在我的站点上,我想确认访问者是否具备cookies和JavaScript功能。维持会话状态在许多情况下都很复杂,而且也要考虑到表单的验证。在我的站点上,我使用了客户端JavaScript进行表单验证,所以要完成验证工作,用户的浏览器必须要具备JavaScript功能。同时,我还使用了Session变量,这就需要访问者的浏览器支持Cookies功能。为了确认这些前提条件的存在,我使用了一段小巧的代码来检查用户是否具备JavaScript和Cookies功能。如果用户不具备这2个条件,就拒绝他们登录网站。在这篇文章里,也介绍我的站点的登录页面代码。

  开始,使用JavaScript创建一个Cookie。然后,再检查这个cookie是否存在。因为我们使用JavaScript来测试用户是否打开Cookies功能,如果他们不具备JavaScript,但却具备Cookies,我们将会得到他们不具备Cookies功能的结果。这个结果是很好的,因为我要求用户同时具备JavaScript和Cookies功能。为了判断用户是否具备JavaScript功能,我在HTML页面上创建了一个隐藏表单域,并且在页面调用事件(onload)中调用一个JavaScript函数来改变这个隐藏表单域的数值。这样,如果表单域的数值改变了,就表示JavaScript是生效的。

  首先,在HTML页面上放置一个隐藏域,并将它介于 ... 之间。



...





  只要这个域的数值是false的,那么我们就知道了用户的浏览器不支持JavaScript。注意,默认值是false,我们编写的JavaScript函数将改变它为true。所以,如果JavaScript生效,这个JavaScript函数将运行,域的数值将变为true。在HTML页面的Body标记中,放置调用JavaScript函数的代码,从而检查cookies(cc):



  如果函数不执行(这种情况仅仅发生在JavaScript不生效时),cc()函数就不会改变隐藏域的数值。cc()函数的代码相当简单,如下:



  注意:在函数外的JavaScript代码行将在页面调入前执行,所以,也就是在cc()函数前被调用。通常,JavaScript代码块应该放在HTML页面的HEAD一节中。

  这样,如果JavaScript生效而Cookies不生效,用户将得到“需要打开cookies功能”的信息提示。如果JavaScript不生效,就不会显示任何信息,这种情况下,我们不能判断cookies功能是否打开。当表单提交时,检查隐藏域cookieexists的数值。如果等于false,那么表示不支持JavaScript。



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Where are the cookies on your computer? Where are the cookies on your computer? Dec 22, 2023 pm 03:46 PM

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Where are the mobile cookies? Where are the mobile cookies? Dec 22, 2023 pm 03:40 PM

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

Detailed explanation of where browser cookies are stored Detailed explanation of where browser cookies are stored Jan 19, 2024 am 09:15 AM

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

See all articles