Välkommen till wF! :)
Här får du något att bita i:
<script type="text/javascript">
/********************************************
script.webforum.nu
Cookiebibliotek skrivet av spango
<gustaf-c@dsv.su.se>
Sprid gärna detta script till nära och kära!
(men lämna kvar dessa rader)
********************************************/
// Cookieklassen
function Cookie(name, value, expires, domain, path, secure){
this.name = name;
this.value = value;
this.expires = expires;
this.domain = domain;
this.path = path;
this.secure = secure;
}
// Metoder för cookieklassen
function set(){
var cookiestr = escape(new String(this.name)) + "=" + escape(new String(this.value));
if(this.expires) cookiestr += "; Expires=" + this.expirationString();
if(this.domain) cookiestr += "; Domain=" + this.domain;
if(this.path) cookiestr += "; Path=" + this.path;
if(this.secure) cookiestr += "; Secure";
document.cookie = cookiestr;
}
function expirationString(){
var ex = this.expires;
if(typeof ex == typeof "string") return ex;
if(typeof ex == typeof 1){
var msPerDag = 1000 * 60 * 60 * 24;
ex = new Date();
ex.setTime(ex.getTime() + (msPerDag * this.expires));
}
return ex.toUTCString ? ex.toUTCString() : ex.toGMTString();
}
function setDomainToCurrent(){
this.domain = self.location.host;
}
function setPathToCurrent(){
var pth = self.location.pathname.substr(self.location.pathname.indexOf ("/"));
if(!pth) pth = "/";
else pth = pth.substring(0, pth.lastIndexOf("/") + 1);
this.path = pth;
}
// Allmäna funktioner
function getCookie(name, alt){
if(!alt) alt = "";
name = escape(name) + "=";
if(document.cookie.indexOf(name) == -1) return alt;
var kaka = document.cookie.substr(document.cookie.lastIndexOf(name));
kaka = kaka.substr(name.length);
var slut = (kaka.indexOf(';') == -1) ? kaka.length : kaka.indexOf(';');
kaka = kaka.substring(0, slut);
return unescape(kaka);
}
function deleteCookie(name, path){
var cookiestr = escape(name) + "=_; Expires=Thursday, 01-Jan-1970 00:00:00 GMT";
if(path) cookiestr += "; Path=" + path;
document.cookie = cookiestr;
}
function getAllCookieNames(){
var ac = document.cookie.split(" ");
var allCookies = new Array();
for(var i = 0; i < ac.length; i++){
var tmp = ac[ i].split("=");
allCookies[ i] = unescape(tmp[0]);
}
return allCookies;
}
// Lägg till metoder
Cookie.prototype.set = set;
Cookie.prototype.expirationString = expirationString;
Cookie.prototype.setDomainToCurrent = setDomainToCurrent;
Cookie.prototype.setPathToCurrent = setPathToCurrent;
// Statiska metoder
Cookie.get = getCookie;
Cookie.remove = deleteCookie;
Cookie.getAllNames = getAllCookieNames;
</script>
<script type="text/javascript">
var endDate = new Date(Cookie.get("countdown")),
elm = null;
window.onload = function(){
elm = document.getElementById("countdown");
if (!endDate.valueOf()){
var tmpDate = new Date();
tmpDate.setDate(tmpDate.getDate() + Math.floor(Math.random() * 31));
new Cookie("countdown", tmpDate, "Wednesday, 01-Jan-2014 00:00:00 GMT").set();
endDate = tmpDate;
}
countdown();
}
function countdown(){
var now = new Date(),
diff = endDate - now,
d = Math.floor(diff / 3600000 / 24),
h = Math.floor((diff / 3600000 / 24 - d) * 24),
m = Math.floor((((diff / 3600000 / 24 - d) * 24) - h) * 60),
s = Math.floor(((((((diff / 3600000 / 24 - d) * 24) - h) * 60) - m) * 60));
if (diff <= 0){
Cookie.remove("countdown");
elm.innerHTML = "";
return;
}
elm.innerHTML = "Tid kvar: " + d + "d" + h + "h" + m + "m" + s + "s";
window.setTimeout("countdown()", 960);
}
</script>
<span id="countdown"></span>