I have a requirement, in jQuery, to show an alert if a cookie does not exist.
I cannot use the jQuery cookie plugin.
I've found a couple of scripts that I may be able to use, however, I cant seem to get them working in my jsfiddles. Can anyone assist me? Or are there other suggestions how to meet this requirement?
http://jsfiddle.net/JustJill54/a3tgP/2/
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
}
return unescape(dc.substring(begin + prefix.length, end));
}
$(document).ready(function(){
var myCookie = getCookie("MyCookie");
if (myCookie == null) {
// do cookie doesn't exist stuff;
alert("Cookie not found.");
}
else {
// do cookie exists stuff
alert("MyCookie");
}
}
http://jsfiddle.net/JustJill54/hYvX3/1/
$(document).ready(function(){
var acookie = ReadCookie("cookiename");
if(acookie.length == 0)
{
alert("Cookie not found.");
}
}
Is there something I am doing wrong that the fiddles above are not creating alerts like I'm expecting?