0

I have a website with 3 html files:

Language selection: media/index.html

EN page: media/en/index.html

CN page: media/cn/index.html

Site files: media/site/

I create a cookie via this script, when a visitor selects a language on my website:

<script type="text/javascript">

    $(function () {

        var url = '';
        var en_page = 'en';
        var cn_page = 'cn';

        if ($.cookie('default_page') != null) {
            if (window.location.href != url + '/' + $.cookie('default_page')) {
                window.location.href = url + '/' + $.cookie('default_page');
            }
        }

        $('#enlink').click(function () {
            $.cookie('default_page', en_page, {expires: 999, path: '/', domain: 'mywebsite.com'});
        });

        $('#cnlink').click(function () {
            $.cookie('default_page', cn_page, {expires: 999, path: '/', domain: 'mywebsite.com'});
        });

    });

</script>

    ....
    ....

    <a href="cn/index.html" id="cnlink">CN</a>
    <br>
<a href="en/index.html" id="enlink">EN</a>

This successfully creates a cookie called "default_page" with a value of either "cn" or "en", then redirects user to mywebsite.com/cn or mywebsite.com/en on subsequent visits. This all works fine.

However, I'm unable to delete this cookie. I need to delete this cookie whenever the user clicks a link to change language in media/en/index.html or media/cn/index.html (ie my actual website, not the language selection page):

<script type="text/javascript">

    function deletecookie(){
        alert('delete');
        $.cookie('default_page', null, {path: '/', domain: 'mywebsite.com'});
    }

</script>

....
....

<a href="../index.html" onclick="deletecookie()">
    Change language
</a>

But this is not working, the moment I click "change language", it still redirects me back to the same page (since the cookie still exists), so I'm unable to access the language page (media/index.html). The function is being called, as I get the alert box, but I checked my browser, which is Firefox, and the cookie mywebsite/default_page is still there.

Thanks for your help.

Naz
  • 2,520
  • 2
  • 16
  • 23
Windbrand
  • 555
  • 4
  • 13
  • 21
  • 1
    Did you try the removeCookie function provided by the plugin itself? $.removeCookie('default_page', { path: '/' }); – Tariqulazam Nov 25 '12 at 21:31

1 Answers1

2

Try changing this

$.cookie('default_page', null, {path: '/', domain: 'mywebsite.com'});

to this

$.cookie('default_page', null);
Greg Smith
  • 2,449
  • 2
  • 24
  • 37
  • Why would that make a difference ? this is an explanation I got from here: http://stackoverflow.com/a/7633502/1100019 "It is the problem of misunderstand of cookie. Browsers recognize cookie values for not just keys also compare the options path & domain. So Browsers recognize different value which cookie values that key is 'name' with server setting option(path='/'; domain='mydomain.com') and key is 'name' with no option." – wadie Nov 25 '12 at 21:28
  • It might not, but it's just a suggestion. – Greg Smith Nov 25 '12 at 21:31