Determine if the browser supports JavaScript and cookies

Source: Internet
Author: User
Tags functions log requires client
Cookie|cookies|javascript| Browser

  Determine if the visitor's browser supports JavaScript and cookies

Many Web sites require clients to do a lot of complex work, such as using client JavaScript for data validation, which requires JavaScript enabled by the client browser, and the use of session variables to record identities and other information that requires a browser cookie enabled. Therefore, it is necessary to determine that these options are open in the user's browser. In my site, I used a string of simple code to implement these functions, check when the user is logged in, and do not allow logins if they do not meet. In this article, I would like to introduce the login page to the wording.

We first create a cookie with JavaScript and check to see if the cookie exists. Because we use JavaScript to do this, if the user does not open JavaScript, but cookies are turned on, we still get the result that the cookies are not open. But this does not conflict with our requirements, anyway we are asking both to open. (If you really just want to know if Cookies are enabled and don't care about JavaScript, there is a way, as we discussed in another article) to determine whether the user is opening JavaScript, I created a hidden from in HTML and then in the onload event, change the value of the hidden form, and if the value is changed, the JavaScript is turned on, otherwise the JavaScript function will not be invoked. (above two features I write in a function)

First, we put a hidden form in HTML and enclose it in <form>...</form>. (Of course, there can be username/password from)

<FORM>
...
<input type= "hidden" name= "CookieExists" value= "false" >
</FORM>

As long as its value is false, it means that JavaScript is not supported by browsers. Note that its initial value is false. Our JavaScript function will change this value

Is true. Write this in the body:

<body >

The contents of CC () are as follows:

<script language= "JavaScript" >
<!-
function cc ()
{
/* 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'll let him know
That cookie need to is enabled to log on.*/
Document. Form1.cookieexists.value = "false"
} else {
/* This sets the value of to true and nothing else would happen,
The user is able to log on*/
Document. Form1.cookieexists.value = "true"
}
}
/* Set A cookie to is sure that one exists.
The This is outside the function*/
Document.cookie = ' Killme ' + escape (' nothing ')
-->
</script>

The functions that this program can achieve are:

1 Pop-up warning message when user JavaScript is turned on and Cookies are turned off

2 When the user JavaScript is turned off, the user cannot directly get the result of the check. (Don't forget, to eject the warning window also need to execute alert this JavaScript statement, then even check out can not prompt), but then the user's from submit, the background of the program will find cookieexists the value of this field is false, which means The JavaScript is off. I don't need to tell you what I'm going to do.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.