国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊(cè) 判斷客戶端是否具備javascript和Cookie功能

判斷客戶端是否具備javascript和Cookie功能

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

cookie|客戶端

在我的站點(diǎn)上,我想確認(rèn)訪問(wèn)者是否具備cookies和JavaScript功能。維持會(huì)話狀態(tài)在許多情況下都很復(fù)雜,而且也要考慮到表單的驗(yàn)證。在我的站點(diǎn)上,我使用了客戶端JavaScript進(jìn)行表單驗(yàn)證,所以要完成驗(yàn)證工作,用戶的瀏覽器必須要具備JavaScript功能。同時(shí),我還使用了Session變量,這就需要訪問(wèn)者的瀏覽器支持Cookies功能。為了確認(rèn)這些前提條件的存在,我使用了一段小巧的代碼來(lái)檢查用戶是否具備JavaScript和Cookies功能。如果用戶不具備這2個(gè)條件,就拒絕他們登錄網(wǎng)站。在這篇文章里,也介紹我的站點(diǎn)的登錄頁(yè)面代碼。

  開(kāi)始,使用JavaScript創(chuàng)建一個(gè)Cookie。然后,再檢查這個(gè)cookie是否存在。因?yàn)槲覀兪褂肑avaScript來(lái)測(cè)試用戶是否打開(kāi)Cookies功能,如果他們不具備JavaScript,但卻具備Cookies,我們將會(huì)得到他們不具備Cookies功能的結(jié)果。這個(gè)結(jié)果是很好的,因?yàn)槲乙笥脩敉瑫r(shí)具備JavaScript和Cookies功能。為了判斷用戶是否具備JavaScript功能,我在HTML頁(yè)面上創(chuàng)建了一個(gè)隱藏表單域,并且在頁(yè)面調(diào)用事件(onload)中調(diào)用一個(gè)JavaScript函數(shù)來(lái)改變這個(gè)隱藏表單域的數(shù)值。這樣,如果表單域的數(shù)值改變了,就表示JavaScript是生效的。

  首先,在HTML頁(yè)面上放置一個(gè)隱藏域,并將它介于 ... 之間。



...





  只要這個(gè)域的數(shù)值是false的,那么我們就知道了用戶的瀏覽器不支持JavaScript。注意,默認(rèn)值是false,我們編寫(xiě)的JavaScript函數(shù)將改變它為true。所以,如果JavaScript生效,這個(gè)JavaScript函數(shù)將運(yùn)行,域的數(shù)值將變?yōu)閠rue。在HTML頁(yè)面的Body標(biāo)記中,放置調(diào)用JavaScript函數(shù)的代碼,從而檢查cookies(cc):



  如果函數(shù)不執(zhí)行(這種情況僅僅發(fā)生在JavaScript不生效時(shí)),cc()函數(shù)就不會(huì)改變隱藏域的數(shù)值。cc()函數(shù)的代碼相當(dāng)簡(jiǎn)單,如下:



  注意:在函數(shù)外的JavaScript代碼行將在頁(yè)面調(diào)入前執(zhí)行,所以,也就是在cc()函數(shù)前被調(diào)用。通常,JavaScript代碼塊應(yīng)該放在HTML頁(yè)面的HEAD一節(jié)中。

  這樣,如果JavaScript生效而Cookies不生效,用戶將得到“需要打開(kāi)cookies功能”的信息提示。如果JavaScript不生效,就不會(huì)顯示任何信息,這種情況下,我們不能判斷cookies功能是否打開(kāi)。當(dāng)表單提交時(shí),檢查隱藏域cookieexists的數(shù)值。如果等于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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

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 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.

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.

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

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.

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

How to find cookies in your browser How to find cookies in your browser Jan 19, 2024 am 09:46 AM

In our daily use of computers and the Internet, we are often exposed to cookies. A cookie is a small text file that saves records of our visits to the website, preferences and other information. This information may be used by the website to better serve us. But sometimes, we need to find cookie information to find the content we want. So how do we find cookies in the browser? First, we need to understand where the cookie exists. in browser

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.

See all articles