开发者

How to detect cookie and javascript with browsers

I am using PHP/JavaScript/MySQL on XAMPP to develop the prototype.开发者_运维问答

I need to use session that in-turn makes use of cookies. Here is the question, how do I know whether or not the user's browser supports cookies or not.

For detecting javascript, I use <noscript></noscript>. Please correct me if I am wrong.

Thank you


You have to set a cookie and test it to see if they're enabled:

<script type = "text/javascript" language = "JavaScript">

   var tmpcookie = new Date();
   chkcookie = (tmpcookie.getTime() + '');
   document.cookie = "chkcookie=" + chkcookie + "; path=/";
    if (document.cookie.indexOf(chkcookie,0) < 0) {
      window.location = 'nocookies.html';
      }
    else {
      window.location = 'cookies.html';
    }

</script>


Here is the question, how do I know whether or not the user's browser supports cookies or not.

If a cookie isn't set, set a cookie and redirect to a page that checks if the cookie is set. If it is, redirect back, otherwise redirect to a "Sorry, we really need cookies" page.

Only do this if you do really need cookies.

For detecting javascript, I use <noscript></noscript>. Please correct me if I am wrong.

Better to build on things that work.


I will choose different style of detecting.

To detect whether javascript is enabled/disabled, i will write some little elements (perhaps div) and i will execute some javascript to remove such elements. Hence, when the javascript is disabled/doesn't exist, those elements will still be there saying that "Please activate your Javascript". On the other hand, those element(s) will be gone since javascript is already remove them. After all, it comes back to your website concept. Some website can't do anything when javascript is not exist/disabled and further they choose to redirect the request into another page (through tag). Some websites still can function but certain feature will not be available, and this concept leads to "warning" technique.

To detect whether cookie is enabled/disabled, just set a cookie using Javascript. In next request, you can check whether such cookie is set or not. If it's set, then both Javascript and Cookie is enabled. If it's not set, then Javascript or Cookie or both is disabled.

Good luck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜