home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-25 | 10.7 KB | 433 lines | [TEXT/LMAN] |
- /*
- THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
- Copyright (C)1996 by Charles River Media. All Rights Reserved.
-
- This applet can only be re-used or modifed by license holders of the
- JavaScript Cookbook CD-ROM. Credit must be given in the source
- code and this copyright notice must be maintained. If you do
- not hold a license to the JavaScript Cookbook, you may NOT
- duplicate or modify this code for your own use.
-
- Use at your own risk. No warranty is given or implied of the suitability
- of this applet for any specific application. Neither Erica Sadun nor
- Charles River Media will be held responsible for any unwanted effects
- due to the use of this applet or any derivative.
-
- COMMON COOKIE ROUTINES USED IN MANY SCRIPTS
- */
-
- // --------------------COOKIE STRING UTILITIES---------------------
-
- // Substitute character string c2 for every c1 in aString
- function subst(aString, c1, c2)
- {
- if (aString == "") return aString
- if (c1 == "") return aString
-
- // avoid infinite recursion when substituting aa for a by
- // providing an offset into the string.
- var argc = subst.arguments.length
- if (argc < 4) {n = 0} else {n = subst.arguments[3]}
-
- // find the first occurence of c1 after the threshold
- var i = aString.indexOf(c1, n)
-
- // stop recursion and return the current string when c1 not found
- if (i < 0) return aString
-
- // extract substrings s1 and s2 around the c1
- var s1 = aString.substring(0, i)
- var s2 = aString.substring(i+c1.length, aString.length)
-
- // recurse with this new string
- return subst(s1+c2+s2, c1, c2, (i+c2.length))
- }
-
- // Strips a string of blanks on either end
- function stripBlanks(aString)
- {
- var tmp = ""+aString
- var bottom = 0
- var top = tmp.length
-
- if (tmp == "") return tmp
-
- while (tmp.substring(bottom, bottom + 1) == " ") bottom++
- if (bottom >= top) return("")
-
- while (tmp.substring(top - 1, top) == " ") top -= 1
-
- return (tmp.substring(bottom, top))
- }
-
- // Count Occurances of a Substring
- function count(aString, aSubstring)
- {
- // initialize counter and offset
- var count = 0
- var offset = 0
- var where = 0
- var tmp = ""+aString
-
- // search until no more found
- while ((offset < tmp.length) &&
- ((where = tmp.indexOf(aSubstring, offset)) >= 0))
- {
- count++
- offset = where+aSubstring.length
- }
-
- // return it
- return count
- }
-
- // Encookie -- code spaces, semicolons and commas
- function encookie(aString)
- {
- var tmp = ""+aString
- tmp = subst(tmp, " ", "%20")
- tmp = subst(tmp, ",", "%2C")
- tmp = subst(tmp, ";", "%3B")
- tmp = subst(tmp, "=", "%3D")
- return tmp
- }
-
- // Decookie -- decode spaces, semicolons and commas
- function decookie(aString)
- {
- var tmp = ""+aString
- tmp = subst(tmp, "%20", " ")
- tmp = subst(tmp, "%2C", ",")
- tmp = subst(tmp, "%3B", ";")
- tmp = subst(tmp, "%3D", "=")
- return tmp
- }
-
- // Search for a substring and chop before it
- function chopit(aString, aSubstring)
- {
- var tmp = ""+aString
- if (tmp.length == 0) return tmp
- if (aSubstring.length == 0) return tmp
-
- var where
- if ((where = tmp.indexOf(aSubstring)) < 0) return tmp
- return (tmp.substring(0, where))
- }
-
- // Search for a substring and lop off everything after it
- function lopit(aString, aSubstring)
- {
- var tmp = ""+aString
- if (tmp.length == 0) return tmp
- if (aSubstring.length == 0) return tmp
-
- var where
- if ((where = tmp.indexOf(aSubstring)) < 0) return tmp
- return (tmp.substring(where+aSubstring.length, tmp.length))
- }
-
-
- //------------------STRING-ARRAY UTILITIES-------------------
-
- // Find the substring at index n, counting 0 to n
- function doIndex(aString, n)
- {
-
- var str=""+aString
-
- // Count until the correct index
- for(var i = 0; i < n; i++)
- {
- var where = str.indexOf(':', 1)
- str = str.substring(where+1, str.length)
- }
-
- // Lop off the end of the string
- return str.substring(0, str.indexOf(':'))
- }
-
- //------------------CUSTOM GMT UTILITY-----------------------------
-
- // Return the 3-letter symbol for month #n
- function month(n)
- {
- var m = "Jan:Feb:Mar:Apr:May:Jun:Jul:Aug:Sep:Oct:Nov:Dec:"
- return doIndex(m, n)
- }
-
- // Return the 3-letter symbol for day #n
- function day(n)
- {
- var d = "Sun:Mon:Tue:Wed:Thu:Fri:Sat:"
- return doIndex(d, n)
- }
-
- // A Custom "GMT" Function
- function myGMT(d)
- {
- tmp = day(d.getDay())+', '+d.getDate()+' '+month(d.getMonth())
- tmp += ' 19'+d.getYear()+' '
-
- if (d.getHours() < 10) tmp += '0'
- tmp += d.getHours()+':'
-
- if (d.getMinutes()< 10) tmp += '0'
- tmp += d.getMinutes()+':'
-
- if (d.getSeconds() < 10) tmp += '0'
- tmp += d.getSeconds()+" GMT"
-
- return tmp
- }
-
- // An Alternate "GMT" Function: NOTE! This--changed for hyphens and no (19)
- function altGMT(d)
- {
- tmp = day(d.getDay())+', '+d.getDate()+'-'+month(d.getMonth())
- tmp += '-'+d.getYear()+' '
-
- if (d.getHours() < 10) tmp += '0'
- tmp += d.getHours()+':'
-
- if (d.getMinutes()< 10) tmp += '0'
- tmp += d.getMinutes()+':'
-
- if (d.getSeconds() < 10) tmp += '0'
- tmp += d.getSeconds()
-
- return tmp
- }
- // --------------------COOKIE DATA UTILITIES---------------------
-
- // Cookie Flattener
- function flatten()
- {
- var tmp = ""+this.name+"="+this.value
- tmp += (this.expires == null) ? "" : "; expires="+this.expires
- tmp += (this.path == null) ? "" : "; path="+this.path
- tmp += (this.domain == null) ? "" : "; domain="+this.domain
- tmp += (this.isSecure) ? "; secure" : ""
- return tmp
-
- }
-
- // Store Cookie in the jar
- function store() {document.cookie = this.flatten()}
-
- // Set Name
- function setName(aName){this.name = encookie(aName)}
-
- // Set Value
- function setValue(aValue){this.value = encookie(aValue)}
-
- // Get Name
- function getName(aName){this.name = decookie(aName)}
-
- // Get Value
- function getValue(aValue){this.value = decookie(aValue)}
-
- // Check for WIN16 -- avoid the date bug
- function isWin16()
- {
- return (navigator.userAgent.toUpperCase().indexOf('WIN16') != -1)
- }
-
- // Expiration Times
- function setExpires(howLong)
- {
- var now = new Date()
- var epoch = isWin16() ? 0 : new Date(0)
- var msecs = now.getTime()
- var offset = now.getTimezoneOffset() * 60000
-
- var t = 0
-
- howLong = howLong.toLowerCase()
-
- // determine how long until expiration
- if (howLong == 'now') t = msecs - (3600000 * 24 * 365) // overkill
- if (howLong == 'hour') t = msecs + (3600000) // 60 minutes
- if (howLong == 'day') t = msecs + (3600000 * 24) // 24 hours
- if (howLong == 'week') t = msecs + (3600000 * 24 * 7) // 7 days
- if (howLong == 'month') t = msecs + (3600000 * 24 * 30) // 30 days
- if (howLong == 'year') t = msecs + (3600000 * 24 * 365) // 365 days
-
- // set the expiration
- exp = isWin16() ? (t - offset) : ((t - epoch.getTime()) - offset)
- now.setTime(exp)
- this.expires = myGMT(now)
- return this
- }
-
- // Turn on security
- function secure(){this.isSecure = true}
-
- // Turn off security
- function unsecure(){this.isSecure = false}
-
- // get rid of a cookie right now
- function crumble()
- {
- this.setExpires('now')
- this.store()
- }
-
- // revive a crumbled cookie
- function uncrumble()
- {
- this.expires = null
- this.store()
- }
-
- // Cookie Constructor
- function JSCCookie(cname, cvalue)
- {
- // Create and initialize cookie slots
- this.name = encookie(cname) // readable and writeable
- this.value = encookie(cvalue) // readable and writeable
- this.expires = null // writeable
- this.isSecure = false // writeable
- this.path = null // writeable
- this.domain = null // writeable
-
- // Set cookie methods
- this.setExpires = setExpires
- this.crumble = crumble
- this.uncrumble = uncrumble
- this.flatten = flatten
- this.store = store
- this.secure = secure
- this.unsecure = unsecure
-
- this.setName = setName
- this.getName = getName
- this.setValue = setValue
- this.getValue = getValue
- }
-
- // --------------------COOKIE JAR UTILITIES---------------------
-
- // find a cookie by partial string
- function findCookie(name)
- {
- num_in_jar = Math.min(document.cookie.length,
- count(document.cookie, ';')+1)
-
- if (num_in_jar != 0)
- {
- var tmp = ""+document.cookie
- for (var i = 0; i < num_in_jar; i++)
- {
- var crumb = stripBlanks(chopit(tmp, ";"))
- tmp = lopit(tmp, ";")
- var cname = decookie(chopit(crumb, "="))
- if (cname.indexOf(name) >= 0) return crumb
- }
- }
- return null
- }
-
- // Fetch a value (by partial string)
- function fetch(name)
- {
- var crumb = findCookie(name)
- if (crumb == null) return null
-
- var cname = decookie(chopit(crumb, "="))
- var cvalue = decookie(lopit(crumb, "="))
- if (cname.indexOf(name) >= 0) return cvalue
- return null
- }
-
- // Fetch a name (by partial string)
- function fetchName(name)
- {
- var crumb = findCookie(name)
- if (crumb == null) return null
-
- var cname = decookie(chopit(crumb, "="))
- var cvalue = decookie(lopit(crumb, "="))
- if (cname.indexOf(name) >= 0) return cname
- return null
- }
-
- // Fetch a particular cookie (by partial string)
- function fetchCookie(name)
- {
- var crumb = findCookie(name)
- if (crumb == null) return null
-
- var cname = decookie(chopit(crumb, "="))
- var cvalue = decookie(lopit(crumb, "="))
- if (cname.indexOf(name) >= 0)
- {
- var c = new JSCCookie(cname, cvalue)
- return c
- }
- return null
- }
-
- // --------------------COOKIE INFO UTILITIES---------------------
-
- // Cookie Information
- function cookiePeek()
- {
- size_in_jar = document.cookie.length
- num_in_jar = Math.min(document.cookie.length,
- count(document.cookie, ';')+1)
-
- if (num_in_jar != 0)
- {
- document.write("<b>Current Cookies:</b> "+
- document.cookie+"<p><blockquote>")
- var tmp = ""+document.cookie
- var crumb, cname, cvalue
- for (var i = 0; i < num_in_jar; i++)
- {
- crumb = chopit(tmp, ";")
- tmp = lopit(tmp, ";")
-
- cname = decookie(chopit(crumb, "="))
- cvalue = decookie(lopit(crumb, "="))
-
- document.write("<b>Name:</b> <FONT COLOR='770000'>"+cname+"</FONT> ")
- document.write("<b>Value:</b> <FONT COLOR='770000'>"+cvalue+"<br></FONT>")
- }
- document.write("</blockquote>")
- }
- else
- document.write("<b>The Cookie Jar is <FONT COLOR='770000'>Empty</FONT>.</b><br>")
-
- document.write('There are a total of '+num_in_jar+' cookies taking up '+
- size_in_jar+' bytes<p>')
- }
-
- // --------------------FORMAT UTILITIES---------------------
-
- function comment(aString)
- {
- document.write("<FONT SIZE=4 COLOR='007777'><b>")
- document.write(aString)
- document.write("</b></FONT><br>")
- }
-
- function observe(aString)
- {
- document.write("<FONT SIZE=3 COLOR='007777'>")
- document.write(aString)
- document.write("</FONT><BR>")
- }
-
- function pre()
- {
- var l = pre.arguments.length
- document.write("<PRE><FONT SIZE=3 COLOR='770000'>")
- for (var i = 0; i < l; i++)
- {
- document.writeln(pre.arguments[i])
- }
- document.write("</FONT></PRE>")
- }
-
-