开发者

Can't create cookies with javascript in IE7/IE8

开发者 https://www.devze.com 2023-03-21 16:36 出处:网络
I\'m using simpleCart.js to setup a cart on the website. The problem is the cookies will not be created in IE7/IE8. So every page refresh we lose the items in the cart.

I'm using simpleCart.js to setup a cart on the website. The problem is the cookies will not be created in IE7/IE8. So every page refresh we lose the items in the cart.

I've been looking at the code and it appears as though sim开发者_如何学GopleCart uses the cookie functions from quirksmode

The only information I can find as to why it would be failing is here: http://www.dynamicdrive.com/forums/showthread.php?t=27197 something to do with how the cookies are set that IE doesn't like.

I modified the quirksmode function to look like this:

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    value = value.replace(/\=/g, '~');
    console.log(name + "=" + value + expires + "; path=/");
    document.cookie = name + "=" + escape(value) + expires + "; path=/";
    console.log(name + "=" + escape(value) + expires + "; path=/");
}

And here is the output:

sc_simpleCart_1=id~c2||name~British%20Curry%20Powder||price~3.5||quantity~4; expires=Thu, 18 Aug 2011 15:58:16 GMT; path=/
sc_simpleCart_1=id%7Ec2%7C%7Cname%7EBritish%2520Curry%2520Powder%7C%7Cprice%7E3.5%7C%7Cquantity%7E4; expires=Thu, 18 Aug 2011 15:58:16 GMT; path=/

I thought that maybe it was something to do with the space characters, but I replaced them with hyphens and it made no difference.

There is actually a live version of the site here: http://www.bitspicy.com/bit-spicy-shop.php try adding an item to the cart and refresh the page in IE8 to see the issue for yourself.

Many thanks, Rich.


OK, I've figured it out.

Basically the simpleCart.js is using a slightly modified version of the quirksmode functions, they were using the escape() and unescape() functions on the cookie data and IE didn't like that at all.

I simply replaced them with the originals and it works!

0

精彩评论

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