Forum Moderators: open

Message Too Old, No Replies

javascript and cookies

         

rhodopsin

8:38 pm on Nov 4, 2004 (gmt 0)

10+ Year Member



I have a really stupid Q - I apoligise now for how basic it must be. But it has got me stuck and I would be so grateful for some advice. I am trying to get this script to work:

[javascriptkit.com...]

Testing whether cookies are enabled.
Use the following technique to detect whether the client's browser has cookies enabled:

Firstly, we use the official property for the task, navigator.cookieEnabled, to help determine whether the browser has cookies enabled. However, we immediately run into a limitation- this property is supported only in IE4+ and NS6+. So, in both cases where the
property returns false or does not exist (in other browsers), we set the variable cookieEnabled to false instead for continued investigation.
Moving on, it's now time to probe all other browsers for cookie availability. The age-old "give it a try" technique works
nicely. By setting a dummy value to document.cookie and immediately probing it to see if the value is retained, we can tell if cookies is enabled on the client's end. The variable cookieEnabled now contains a Boolean value (true/false)indicating the client's cookie support. Nice and crunchy!

<script type="text/javascript">
var cookieEnabled=(navigator.cookieEnabled)? true : false

//if not IE4+ nor NS6+
if (typeof navigator.cookieEnabled=="undefined" &&!cookieEnabled){
document.cookie="testcookie"
cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
}

//if (cookieEnabled) //if cookies are enabled on client's browser
//do whatever

</script>

So, the above is what I have cut and paste off the web. Below is my own code, based on that above, which I cannot get to work. It is a redirect based on whether cookies are enabled or not.

<script type="text/javascript">

if (cookieEnabled=="true")
{
location.replace("English.html");
}else
{
location.replace("korean.html");
</script>

Would be so grateful for any help. I wouldn;t ask normally but I have been trying for a fair little while to get it to work with no success.

StupidScript

10:31 pm on Nov 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SO close! :)

Copy-and-paste this:

<script type="text/javascript">
var cookieEnabled=(navigator.cookieEnabled)? true : false

//if not IE4+ nor NS6+
if (typeof navigator.cookieEnabled=="undefined" &&!cookieEnabled){
document.cookie="testcookie"
cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
}

if (cookieEnabled) {
location.replace("English.html");
}
else {
location.replace("korean.html");
}
</script>

It's nearly exactly the same as what you had before, with two exceptions:

1) (cookieEnabled) instead of (cookieEnabled=="true")
2) Place a closing curly bracket at the end of your script

That's it!

rhodopsin

11:35 pm on Nov 4, 2004 (gmt 0)

10+ Year Member



Thanks ever so much mate. I have got the code above working. I am ever so grateful. However, I am having problems integrating the principles of the above script into my current script. Which I have placed in this post (have edited it a bit to leave only the relevant lines in it - it had a lot of clutter that was doing things not relevent to this cookie issue).

The crucial lines of my script (in psuodocode in places. The actual script is further down the post):

var sblocked="UK";
var sblocklang="es";

if(sblocklang.indexOf(language)==-1&&sblocked.indexOf(sCountryCode)==-1&&(cookieEnabled))
{redirect url 1}
else
{redirect url 2}

IF the language of the visitor is NOT spanish (es) AND the country of the visitor is NOT uk AND the visitor has cookies enabled redirect url 1
else redirect url 2.

This code works fine apart from the cookie component - it doesn't seem to be picking up on whether cookies are turned on or off.
It can redirect to url 1 even if cookies are not enabled on the browser (for instances where the language is not spanish and the country of the visitor is not the uk). Why this is I don;t know. Help!

Once again many thanks.

The script:

<SCRIPT LANGUAGE="JavaScript1.2">
var sblocked="UK";
var sblocklang="es";

var cookieEnabled=(navigator.cookieEnabled)? true : false
//if not IE4+ nor NS6+
if (typeof navigator.cookieEnabled=="undefined" &&!cookieEnabled)
{
document.cookie="testcookie"
cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
}

if(sblocklang.indexOf(language)==-1&&sblocked.indexOf(sCountryCode)==-1&&(cookieEnabled))
{
location.replace("English.html");
}else
{
location.replace("korean.html");
}
</script>

StupidScript

12:58 am on Nov 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If all three of your conditions are met, go to English, else go to korean.

NOT sblocklang.indexOf(language)
NOT sblocked.indexOf(sCountryCode)
YES cookieEnabled

If any ONE of these are not met, it goes to korean.

YES sblocklang.indexOf(language)
NOT sblocked.indexOf(sCountryCode)
YES cookieEnabled

NOT sblocklang.indexOf(language)
YES sblocked.indexOf(sCountryCode)
YES cookieEnabled

NOT sblocklang.indexOf(language)
NOT sblocked.indexOf(sCountryCode)
NOT cookieEnabled

would all go to korean.

This is not happening like that? What happens when you change it to!cookieEnabled? Does it consistently go to English, still, or do they all go to korean?

rhodopsin

11:03 am on Nov 6, 2004 (gmt 0)

10+ Year Member



still working on it mate - something a bit strange going on. Getting different results in IE and Firefox. Will post again when i at least have more of a grasp what the problems are.