Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2008
    Posts
    58
    Thanks
    2
    Thanked 1 Time in 1 Post

    3rd Party Cookie Test

    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!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    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.

    Code:
    <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)
    Last edited by Philip M; 06-23-2009 at 09:31 AM.

  • #3
    New Coder
    Join Date
    Dec 2008
    Posts
    58
    Thanks
    2
    Thanked 1 Time in 1 Post
    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)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by thecaligarmo View Post
    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.

    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.

  • #5
    New Coder
    Join Date
    Dec 2008
    Posts
    58
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Philip M View Post
    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?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    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.

  • #7
    New Coder
    Join Date
    Dec 2008
    Posts
    58
    Thanks
    2
    Thanked 1 Time in 1 Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •