With the slow death of flash I have decided to build my game on a javascript MVC framework. The core is using the Common JS library Inject.
This mornings project was creating a cookie manager. I stole and modified some basic cookie functions from W3Schools, and put it into the Common JS structure. Happy Scripting.
Here is the code to call it.
////////////////////////////////////////////////////
var Cookie = require("modules.model.Cookie");
var App = function()
{
this.Cookie = new Cookie();
//set the cookie
this.Cookie.setCookie("UserName", "Bob");
//Get the cookie
var userName = this.Cookie.getCookie("UserName");
alert("Username = " + userName);
//is the cookie
var isUser = this.Cookie.isCookieValue("UserName", "Bob");
alert("Is Bob the User? " + isUser);
};
///////////////////////////////////////////////////
Here is the module
/////////////////////////////////////////////////
var Cookie = function()
{
var self = this;
this.DEFAULT_EXP = 30; //days
this.setCookie = function(c_name,value,exdays)
{
var exdate=new Date();
if(!exdays)
{
exdays = self.DEFAULT_EXP;
}
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
};
this.getCookie = function(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
};
this.isCookieValue = function(name, value)
{
debugger;
var cookie = self.getCookie(name);
if(cookie===null || cookie==="")
{
return false;
}
else
{
if(cookie === value)
{
return true;
}
else
{
return false;
}
}
};
};
module.exports = Cookie;
///////////////////////////////////////////////