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.