25. oktober 1999 - 22:09
Der er
2 kommentarer og
1 løsning
Cookies - username - visits
Ok dette script virker ikke som jeg vil have det til:
<HTML><HEAD><TITLE>test</TITLE>
<SCRIPT language=javascript>
<!--
function setcookie(name,value) {
today = new Date;
expires = new Date(today.getTime() + 30 * 24 * 60 * 60 * 1000);
document.cookie=name + "=" + escape(value) + "; expires=" + expires
}
function getcookie(name) {
cookielist=document.cookie.split("; ");
for ( count = 0 ; count < cookielist.length ; count++ ) {
if ( cookielist[count].split("=")[0] == name )
return unescape(cookielist[count].split("=")[1])
}
return null;
}
function showvisits() {
visits = getcookie("visits")
if ( visits == null ) visits = 0;
visits++;
document.write(visits);
setcookie("visits",visits);
}
function showName() {
navn = getcookie("navn")
if ( navn == null ) {
navn = prompt("Skriv dit navn:")
setcookie("navn",navn);
}
document.write(navn);
}
//-->
</SCRIPT>
</HEAD>
User:
<SCRIPT>showName()</SCRIPT>
Visits:
<SCRIPT>showvisits()</SCRIPT>
</BODY></HTML>
Jeg vil gerne have det til at sprøge om navnet når man går ind på siden(det gør den)
og når man har skrevet det navn bliver det gemt i en cookie på ens computer så når man går ind på siden igen skal man ikke skrive navn + at man kan se hvormange gange man har været derind...
-Kan I hjælpe mig, måske komme med andre forslag!
/MF
25. oktober 1999 - 23:41
#1
prøv det her:
<SCRIPT>
<!--
var cookieVisitID = 'my_Bigvisit';
var cookieNumVisitID = 'my_NumBigvisit';
var gLastVisit;
var gNumVisits;
SetLastVisit(); // Execute when loading page
function GetLastVisit ()
{
if ( gLastVisit == "")
{
return "This is your first visit on CyberEsben's GIGA homepage";
}
else
{
var oldVisitDate = new Date(gLastVisit);
return "The last time you were here was "
+ gLastVisit +". You have been looking at my homepage "
+ gNumVisits + " time" +(gNumVisits>1 ? "s" :" Times before ")
+" before."
}
}
function SetLastVisit (name, value)
{
var newVisitDate = new Date();
var expDate = new Date ();
var numVisits = 0;
// The expDate is the date when the cookie should
// expire, we will keep it for a year
expDate.setTime( expDate.getTime() + (365 * 24 * 60 * 60 * 1000) );
// Info about last visit
if (GetCookie (cookieVisitID) != null)
gLastVisit = GetCookie (cookieVisitID);
else
gLastVisit = "";
if (GetCookie (cookieNumVisitID) != null)
gNumVisits = GetCookie (cookieNumVisitID);
else
gNumVisits = 0;
// Use eval() to convert a string to a number
numVisits = eval(gNumVisits) +1;
// Store info about this visit
SetCookie( cookieVisitID, newVisitDate, expDate);
SetCookie( cookieNumVisitID, numVisits, expDate);
}
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
// Function to delete a cookie. (Sets expiration date to current date/time)
// name - String object containing the cookie name
//
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1); // This cookie is history
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
// -->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
<!-- hide
var username = GetCookie('username');
if (username == null) {
username = prompt('What is your name? I will remember it for the future.',"");
if (username == null) {
alert('It`s OK that you don\'t write your name.');
username = 'My Friend';
} else {
// don't set the username cookie.... because the user refused to tell you.
// they will be prompted next time....
pathname = location.pathname;
myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
// set expiry date to 1 year from now.
var largeExpDate = new Date ();
largeExpDate.setTime(largeExpDate.getTime() + (30 * 24 * 3600 * 1000));
SetCookie('username',username,largeExpDate,myDomain);
}
}
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0)
break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" +
expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
var today = new Date ()
var hrs = today.getHours();
document.writeln("Your name is");
if (hrs < 5)
document.write ("<FONT SIZE=\"5\" COLOR=\"#0000FF\">"+ username +",</FONT><BLINK><font COLOR=\"#FF0000\"> shouldn\'t you be in bed?</FONT></BLINK>");
else if (hrs < 12)
document.write ("<FONT SIZE=\"5\" COLOR=\"#0000FF\">"+ username +",</FONT><BLINK><font COLOR=\"#FF0000\"> remember to eat your breakfast!</FONT></BLINK>");
else if (hrs <= 18)
document.write ("<FONT SIZE=\"5\" COLOR=\"#0000FF\">"+ username +",</FONT><BLINK><font COLOR=\"#FF0000\"> remember to eat your l(a)unch!</FONT></BLINK>");
else if (hrs <= 24)
document.write ("<FONT SIZE=\"5\" COLOR=\"#0000FF\">"+ username +",</FONT><BLINK><font COLOR=\"#FF0000\"> remember to goto to bed!</FONT></BLINK>");
if (hrs < 5) document.write("");
if (hrs > 5) document.write("");
// Now you can tell the other browsers -->
</SCRIPT>
<SCRIPT>
<!-- Hide
function pushbutton() {
alert("Welcome " + username +"!!");
}
// end hiding-->
</SCRIPT>
<SCRIPT>document.write(GetLastVisit())</SCRIPT>