"notes" and "coffee counter"

L

Luke

Hi,

I had the need to keep track of how many coffees I'm having per day, as
well as a small notes application.


Here is the code I wrote for that,

hope someone finds it useful too.

kind regards,

Luke

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parts taken from David Flanagan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<script language = "javascript">
var value = 0;
var allCooks;
var pos, start, end;

function getNumCoffees() {
var cValue;
allCooks = document.cookie;
pos = allCooks.indexOf("numCoffees=");
if (pos == -1) {
cValue = 0;
document.cookie = "numCoffees=" + cValue;
}
else {
start = pos + 11;
end = allCooks.indexOf(";", start);
if (end == -1) {end=allCooks.length;}
cValue = parseInt(allCooks.substring(start,end));
}
return cValue;
}

function haveACoffee() {
value = getNumCoffees();
value++;
document.cookie = "numCoffees=" + value;
document.location = "test.html";
}

function setToZero() {
value = 0;
document.cookie = "numCoffees=" + value;
document.location = "test.html";
}
</script>

<!-- IMPLEMENTATION -->

<script>document.write(getNumCoffees());</script>
<button class="small" onclick="haveACoffee();">Coffee!</button>
<button class="small" onclick="setToZero();">set zero</button>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<script language = "javascript">
var value = 0;
var allCooks, allNotes;
var pos, start, end;

function getNotes() {
var cValue;
allCooks = document.cookie;
pos = allCooks.indexOf("notes=");
if (pos == -1) {
cValue = 0;
document.cookie = "notes=" + cValue;
}
else {
start = pos + 6;
end = allCooks.indexOf(";", start);
if (end == -1) {end=allCooks.length;}
cValue = allCooks.substring(start,end);
}
allNotes = cValue.split(":");
return allNotes;
}

function writeNote(x) {
oNotes = getNotes().toString();
oNotes = oNotes.replace(/,/g,":");
var nextYear = new Date();
nextYear.setFullYear(nextYear.getFullYear() + 1 );
document.cookie = "notes=" + oNotes + ":" + x + "; expires=" + nextYear.toGMTString();
document.location = "notes.html";
}

function deleteNote(x) {
dNotes = getNotes();
dNotes.splice(x,1);
oNotes = dNotes.toString();
oNotes = oNotes.replace(/,/g,":");
document.cookie = "notes=" + oNotes;
document.location = "notes.html";
}
</script>

<!-- IMPLEMENTATION -->

<form name="newNote" method="post">
<textarea name="noteBody" cols="10" rows="2"></textarea>
<br>
<input type="submit" value="add note" onclick="writeNote(document.newNote.noteBody.value);">
</form>
Note items
<table border=1>
<script>
for(i=0;i<getNotes().length;i++) {
if(getNotes() != 0) {
var s = getNotes()
document.write("<tr class='note'><td>");
document.write(i + "&nbsp;&nbsp;" + s);
document.write("</td></tr>");
}
}
</script>
<tr><td>
Delete note:
<form name="latest" onsubmit="deleteNote(document.latest.aNum.value);" method="post">
<input type="text" name="aNum" size="2">
<input type="submit" value="Go">
</form>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top