Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asking a lot here... cookies!

Status
Not open for further replies.

sonicsnail

Technical User
Nov 10, 2001
48
IN
Hi all,

I have a page that contains a textarea, and lots of checkboxes.. the user ticks some of the boxes, data is passed into the textarea, which they then copy/paste into an application.

There is some default content in the textarea from the start, which the user updates every time.. (mostly their own details).

I'd like the default content (or some of it) to be written in by a cookie. ie. they enter their name etc once into a popup, and everytime they access the page, it sets the default data up with their own details.

Can anyone point me in the right direction? - I know this is a pretty tricky one, but any online resources would be appreciated.
 
can no one point me in the direction of a helpful url?
 
Can't claim originality for this as I found it in a book "Mastering JavaScript and Jscript" by James Jaworski, sybex books, but it may be able to be adapted for your purposes.

<HTML>
<HEAD>
<TITLE>To Do</TITLE>
<SCRIPT LANGUAGE=&quot;JavaScript&quot; SRC=&quot;cookie.js&quot;><!--
// --></SCRIPT>
<SCRIPT LANGUAGE=&quot;JavaScript&quot; SRC=&quot;notes.js&quot;><!--
// --></SCRIPT>
</HEAD>
<BODY>
<FORM>
<H3>To Do:
<INPUT NAME=&quot;save&quot; TYPE=&quot;BUTTON&quot; VALUE=&quot;Save&quot;
onClick=&quot;saveNotes()&quot;></H3>
<TEXTAREA NAME=&quot;notes&quot; ROWS=&quot;10&quot; COLS=&quot;40&quot;
VALUE=&quot;&quot;></TEXTAREA>
</FORM>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;><!--
loadNotes()
// --></SCRIPT>
</BODY>
</HTML>

NOTES.JS
function loadNotes() {
var cookie=document.cookie
if(nameDefined(cookie,&quot;toDo&quot;)) {
todo=getCookieValue(cookie,&quot;toDo&quot;)
todo=decode(todo)
}else todo=&quot;&quot;
document.forms[0].notes.value=todo
}
function saveNotes() {
todo=window.document.forms[0].notes.value
todo=encode(todo)
var newCookie = &quot;toDo=&quot;+todo+&quot;; expires=&quot;
newCookie += &quot;Thursday, 20-Feb-2048 00:01:00 GMT&quot;
document.cookie=newCookie
}
function encode(s) {
t=&quot;&quot;
for(var i=0;i<s.length;++i) {
ch=s.charAt(i)
if(ch==&quot;/&quot;) t += &quot;//&quot;
else if(ch==&quot; &quot;) t += &quot;/b&quot;
else if(ch==&quot;,&quot;) t += &quot;/.&quot;
else if(ch==&quot;;&quot;) t += &quot;/:&quot;
else if(ch==&quot;\n&quot;) t += &quot;/n&quot;
else if(ch==&quot;\r&quot;) t += &quot;/r&quot;
else if(ch==&quot;\t&quot;) t += &quot;/t&quot;
else if(ch==&quot;\b&quot;) t += &quot;/b&quot;
else t += ch
}
return t
}
function decode(s) {
// Decode the encoded cookie value
t=&quot;&quot;
if(s==null) return t
for(var i=0;i<s.length;++i) {
var ch=s.charAt(i)
if(ch==&quot;/&quot;) {
++i
if(i<s.length){
ch=s.charAt(i)
if(ch==&quot;/&quot;) t += ch
else if(ch==&quot;.&quot;) t += &quot;,&quot;
else if(ch==&quot;:&quot;) t += &quot;;&quot;
else if(ch==&quot;n&quot;) t += &quot;\n&quot;
else if(ch==&quot;r&quot;) t += &quot;\r&quot;
else if(ch==&quot;t&quot;) t += &quot;\t&quot;
else if(ch==&quot;b&quot;) t += &quot; &quot;
}
}else t += ch
}
return t
}

COOKIE.JS
function nameDefined(c,n) {
var s=removeBlanks(c)
var pairs=s.split(&quot;;&quot;)
for(var i=0;i<pairs.length;++i) {
var pairSplit=pairs.split(&quot;=&quot;)
if(pairSplit[0]==n) return true
}
return false
}
function removeBlanks(s) {
var temp=&quot;&quot;
for(var i=0;i<s.length;++i) {
var c=s.charAt(i)
if(c!=&quot; &quot;) temp += c
}
return temp
}
function getCookieValue(c,n) {
var s=removeBlanks(c)
var pairs=s.split(&quot;;&quot;)
for(var i=0;i<pairs.length;++i) {
var pairSplit=pairs.split(&quot;=&quot;)
if(pairSplit[0]==n) return pairSplit[1]
}
return &quot;&quot;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top