...

View Full Version : 3rd Party Cookie Test



thecaligarmo
06-23-2009, 12:11 AM
I've been looking around the forums and on google and can't seem to find a good way to get this test to work.

What: I want to be able to test whether a client has 3rd party cookies enabled.

I know 3rd party cookies are touchy, so here is the why:
Inside my site I have an iframe to another site that I (partially) control. [I dont control some of the javascript functions, and the HTML layout, but I do own most of the content and am the designer involved]. One of the things on this site [that I don't control] is a script that tests whether cookies are enabled or not. Since it is within an iframe, it is a 3rd party cookie and thus generates an error message in the form of an alert stating that cookies are not properly enabled. I want to be able to test to see if 3rd party cookies are enabled so that if they are not then I will not generate the iframe and instead display a phone number to contact else it will display the proper iframe. The iframe is a MUCH prefered method than just handing out my phone number to strangers, so any help would be beneficial. Thanks!

Philip M
06-23-2009, 09:25 AM
To test whether cookies are enabled, you set a non-empty cookie and then retrieve it. If the retrieved cookie value is the same as you've just set, then cookies are enabled. If the retrieved value is an empty string, then cookies are disabled.


<script type="text/javascript" >

document.cookie = "TemporaryTestCookie=yes;";
if(document.cookie.indexOf("TemporaryTestCookie=") == -1) {
alert("Cookies are not enabled in this browser ");
}
else {
alert ("Cookies are enabled in this browser ")
}

</script>



"If you can't explain it simply, you don't understand it well enough” - Albert Einstein (German born American Physicist who developed the special and general theories of relativity. Nobel Prize for Physics in 1921. 1879-1955)

thecaligarmo
06-23-2009, 06:49 PM
All alert does is lets me know whether or not I have 3rd party cookies enabled. I want to be able to test for it with the following necessities:
without creating cookies on another domain (without actually creating a 3rd party cookie and testing for it)
without alerting anything (I dont want the user to know that I am testing)

Philip M
06-23-2009, 07:16 PM
All alert does is lets me know whether or not I have 3rd party cookies enabled. I want to be able to test for it with the following necessities:
without creating cookies on another domain (without actually creating a 3rd party cookie and testing for it)
without alerting anything (I dont want the user to know that I am testing)

You do not actually have to show the alerts! They are there for testing purposes. :rolleyes:

If cookies are enabled - proceed to load iframe
If cookies are not enabled - display the phone number.

I would have thought that it was obvious that there is no way to find out if cookies are enabled except by creating a cookie and testing for it, as I have indicated. You can of course erase it immediately afterwards.

thecaligarmo
06-24-2009, 07:20 PM
If cookies are enabled - proceed to load iframe
If cookies are not enabled - display the phone number.


I know that, but this only shows whether 1st party cookies are enabled, not 3rd party cookies. Just because a 1st party cookie is enabled does not automatically make 3rd parties enabled.

So if I were to create a cookie on the other domain (domain#2) how can I check whether that cookie exists while the user is on domain#1? Since they are different domains I can't directly just check for the existence of the cookie. Does document.cookie allow retrieval of cross-domain cookies?

Philip M
06-24-2009, 08:05 PM
Bear in mind that many/most people will have blocked third-party cookies.

To test whether or not a browser will accept third-party cookies, put a web page containing the JavaScript shown in my earlier post on a different domain. Then, make a web page for your regular domain that contains an IFRAME that loads the web page of that other domain. Now, whenever the web page with the IFRAME is loaded into a browser, a third-party cookie test will be done.

thecaligarmo
06-25-2009, 09:08 PM
So I took what you said and changed it a little in order for it to work. Here is what I did:

I created an iframe on domain#1. On domain#2 I tested to see if we could write a cookie. If we could I left it alone. If we could not I created an iframe on domain#2 that went to a different page on domain#1. This page created a cookie that said '3rdPartyCookieDisabled'. [For some reason this cookie was created whether or not 3rd party cookies were disabled as we are currently 'on that domain' and thus is treated as a 1st party cookie.]

Then on the original page I could just test for this cookie. The only issue with this fix is that you have to run this test before you test for the cookie which makes the first page not show up correctly sometimes, but that is not as much of an issue for me.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum