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.

1
  • 1
    Did you try the removeCookie function provided by the plugin itself? $.removeCookie('default_page', { path: '/' }); Commented Nov 25, 2012 at 21:31

1 Answer 1

2

Try changing this

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

to this

$.cookie('default_page', null);
Sign up to request clarification or add additional context in comments.

2 Comments

Why would that make a difference ? this is an explanation I got from here: 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."
It might not, but it's just a suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.