Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q1582 How can I prevent the browser from loading a certain page if the user won't or can't accept cookies?

irt.org | Knowledge Base | JavaScript | Cookie | Q1582 [ previous next ]

Q1582 How can I prevent the browser from loading a certain page if the user won't or can't accept cookies?

Try the following:

<script language="JavaScript"><!--
function getCookie(name) {
  var cookies = document.cookie;
  var start = cookies.indexOf(name + '=');
  if (start == -1) return null;
  var len = start + name.length + 1;
  var end = cookies.indexOf(';',len);
  if (end == -1) end = cookies.length;
  return unescape(cookies.substring(len,end));
}

function setCookie(name, value, expires, path, domain, secure) {
  value = escape(value);
  expires = (expires) ? ';expires=' + expires.toGMTString() :'';
  path    = (path)    ? ';path='    + path                  :'';
  domain  = (domain)  ? ';domain='  + domain                :'';
  secure  = (secure)  ? ';secure'                           :'';

  document.cookie =
    name + '=' + value + expires + path + domain + secure;
}

function deleteCookie(name, path, domain) {
  var expires = ';expires=Thu, 01-Jan-70 00:00:01 GMT';
  (path)    ? ';path='    + path                  : '';
  (domain)  ? ';domain='  + domain                : '';

  if (getCookie(name))
    document.cookie = name + '=' + expires + path + domain;
}

function isCookieEnabled() {
  if (document.all) {
    if (!navigator.cookieEnabled) {
      alert('Please enable cookies');
      return false;
    }
    else return true;
  }
  else {
    setCookie('temp','temp');
    var temp = getCookie('temp');
    if (!temp) {
      alert('Please enable cookies');
      return false;
    }
    else return true;
  }
}
//--></script>

<a href="cookiepage.html" onClick="return isCookieEnabled()">Next page</a>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.