Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery $.cookie (domain:) causing cookie not to set

Tags:

jquery

Hi the below code sets a Cookies based on a link ID clicked (that's the value), this works but what o need to do it set the domain, i have read how to do this by setting domain in the values, when i set the domin it will not set the cookie at all

Working:

jQuery(document).ready(function(){
    $(".htabs a").click(function(e){
        $.cookie('siteVistedCookie', $(this).attr("id"), { expires: 7, path: '/'});
        });
    });

Not working

jQuery(document).ready(function(){
    $(".htabs a").click(function(e){
        $.cookie('siteVistedCookie', $(this).attr("id"), { expires: 7, path: '/', domain: 'www.xample.com' });
        });
    });
like image 256
odd Avatar asked Dec 27 '22 20:12

odd


2 Answers

Just guessing here, but if the domain you are trying to set is not the domain of the site the page is hosted on, then the domain (host) will not be set in the cookie to what you type in the as the domain in the cookie setter.

In other words, if the domain is not the domain of the hosted page, you are trying to set a third-party cookie which most modern browsers do not allow.

Unless your site is xample.com and you are running the page as xample.com/testcookie.html (or whatever you are calling your test page), the domain (host) of the cookie will not be set to xample.com.

like image 192
jk. Avatar answered Dec 30 '22 09:12

jk.


Run the script without the domain setting, then look at the cookie it sets. What domain does it show? Does it match up with what you are putting into the domain property?

Cookie domains must be set with the same (sub) domain the page is on. For instance, if your page is on

http://www.somesite.com/page.html

You can set your cookie domain as

www.somesite.com <-- this will specifically set it for www subdomain

.somesite.com <-- this will allow it to be set for any subdomain of somesite.com

like image 25
Crayon Violent Avatar answered Dec 30 '22 09:12

Crayon Violent