亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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

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

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

cookie|客戶端

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

  開始,使用JavaScript創(chuàng)建一個Cookie。然后,再檢查這個cookie是否存在。因為我們使用JavaScript來測試用戶是否打開Cookies功能,如果他們不具備JavaScript,但卻具備Cookies,我們將會得到他們不具備Cookies功能的結(jié)果。這個結(jié)果是很好的,因為我要求用戶同時具備JavaScript和Cookies功能。為了判斷用戶是否具備JavaScript功能,我在HTML頁面上創(chuàng)建了一個隱藏表單域,并且在頁面調(diào)用事件(onload)中調(diào)用一個JavaScript函數(shù)來改變這個隱藏表單域的數(shù)值。這樣,如果表單域的數(shù)值改變了,就表示JavaScript是生效的。

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



...





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



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



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

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

Hot Topics

PHP Tutorial
1488
72
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.

Frequently Asked Questions and Solutions about Cookie Settings Frequently Asked Questions and Solutions about Cookie Settings Jan 19, 2024 am 09:08 AM

Common problems and solutions for cookie settings, specific code examples are required. With the development of the Internet, cookies, as one of the most common conventional technologies, have been widely used in websites and applications. Cookie, simply put, is a data file stored on the user's computer that can be used to store the user's information on the website, including login name, shopping cart contents, website preferences, etc. Cookies are an essential tool for developers, but at the same time, cookie settings are often encountered

See all articles