Determine whether the visitor's browser supports JavaScript and Cookies.
Many websites require the client to do a lot of complex work, such as: using client-side JavaScript to verify data validity, which requires JavaScript enabled in the client's browser; using Session variables to record identities, etc. Information, requires browser Cookies enabled. Therefore, it is necessary to ensure that these options are turned on in the user's browser. In my website, I use a bunch of concise codes to implement these functions, check when the user logs in, and do not allow the user to log in if they do not meet the requirements. In this article, I will introduce how to write this login page.
We first use JavaScript to create a Cookie, and then check whether the Cookie exists. Since we use JavaScript to do this, if the user does not turn on JavaScript but turns on Cookies, we will still get the result that Cookies are not turned on. But this does not conflict with our requirements. Anyway, we require both to be opened. (If you really just want to know whether Cookies is enabled and don't care about JavaScript, there is a way. We will discuss it in another article.) To determine whether the user has JavaScript turned on, I created a hidden from in the html, and then called it in the onload event. A JavaScript function changes the value of the hidden form. If the value is changed, it means that JavaScript is turned on, otherwise the JavaScript function will not be called. (I wrote the above two functions in one function)
First, we put a hidden form in the html, enclosed by <form>...</form>. (Of course, there can also be username/password from)
<FORM>
...
<input type="hidden" name="cookieexists" value="false">
</FORM>
As long as its value is false, it means that the browser does not support JavaScript. Note that its initial value is false. Our JavaScript function will change this value
to true. Write this in BODY:
<body onload="cc()">
The content of cc() is as follows:
The following is a quotation fragment:
<script language="JavaScript">
<!-
functioncc()
{
/* check for a cookie */
if (document.cookie == "")
{
/* if a cookie is not found - alert user -
change cookieexists field value to false */
alert("COOKIES need to be enabled!");
/* If the user has Cookies disabled an alert will let him know
that cookies need to be enabled to log on.*/
document.Form1.cookieexists.value="false"
} else {
/* this sets the value to true and nothing else will happen,
the user will be able to log on*/
document.Form1.cookieexists.value="true"
}
}
/* Set a cookie to be sure that one exists.
Note that this is outside the function*/
document.cookie = 'killme' + escape('nothing')
// -->
</script>
The functions that this program can implement are:
1. When the user's JavaScript is turned on and Cookies is turned off, a warning message will pop up.
2. When the user's JavaScript is turned off, the user cannot directly get the check results. (Don't forget that to pop up a warning window, you also need to execute the alert JavaScript statement. At this time, even if you check it, you will not be prompted). However, after the user's from is submitted, the background program will find that the value of the cookieexists field is false, so It means JavaScript is turned off. You don’t need me to tell you what you’re going to do in the future, right?