home *** CD-ROM | disk | FTP | other *** search
/ JavaScript CD Cookbook / JavaScript CD Cookbook.iso / mac / MACFILES / COOKIES / COMMON / COOKIE.CMN < prev   
Encoding:
Text File  |  1996-05-25  |  10.7 KB  |  433 lines  |  [TEXT/LMAN]

  1. /*
  2.     THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
  3.     Copyright (C)1996 by Charles River Media.  All Rights Reserved.
  4.     
  5.     This applet can only be re-used or modifed by license holders of the
  6.     JavaScript Cookbook CD-ROM.  Credit must be given in the source
  7.     code and this copyright notice must be maintained. If you do
  8.     not hold a license to the JavaScript Cookbook, you may NOT
  9.     duplicate or modify this code for your own use.
  10.  
  11.     Use at your own risk. No warranty is given or implied of the suitability 
  12.     of this applet for any specific application. Neither Erica Sadun nor 
  13.     Charles River Media will be held responsible for any unwanted effects 
  14.     due to the use of this applet or any derivative. 
  15.  
  16.     COMMON COOKIE ROUTINES USED IN MANY SCRIPTS
  17. */
  18.  
  19. // --------------------COOKIE STRING UTILITIES---------------------
  20.  
  21. // Substitute character string c2 for every c1 in aString
  22. function subst(aString, c1, c2)
  23. {
  24.     if (aString == "") return aString
  25.     if (c1 == "") return aString
  26.     
  27.     // avoid infinite recursion when substituting aa for a by
  28.     // providing an offset into the string.
  29.     var argc = subst.arguments.length
  30.     if (argc < 4) {n = 0} else {n = subst.arguments[3]}
  31.  
  32.     // find the first occurence of c1 after the threshold
  33.     var i = aString.indexOf(c1, n)
  34.     
  35.     // stop recursion and return the current string when c1 not found
  36.     if (i < 0) return aString
  37.     
  38.     // extract substrings s1 and s2 around the c1
  39.     var s1 = aString.substring(0, i)
  40.     var s2 = aString.substring(i+c1.length, aString.length)
  41.     
  42.     // recurse with this new string
  43.     return subst(s1+c2+s2, c1, c2, (i+c2.length))
  44. }
  45.  
  46. // Strips a string of blanks on either end
  47. function stripBlanks(aString)
  48. {
  49.     var tmp = ""+aString
  50.     var bottom = 0
  51.     var top = tmp.length
  52.     
  53.     if (tmp == "") return tmp
  54.     
  55.     while (tmp.substring(bottom, bottom + 1) == " ") bottom++
  56.     if (bottom >= top) return("")
  57.     
  58.     while (tmp.substring(top - 1, top) == " ") top -= 1
  59.     
  60.     return (tmp.substring(bottom, top))
  61. }
  62.  
  63. // Count Occurances of a Substring
  64. function count(aString, aSubstring)
  65. {
  66.   // initialize counter and offset
  67.   var count = 0
  68.   var offset = 0
  69.   var where = 0
  70.   var tmp = ""+aString
  71.  
  72.   // search until no more found
  73.   while ((offset < tmp.length) &&
  74.            ((where = tmp.indexOf(aSubstring, offset)) >= 0))
  75.   {
  76.       count++
  77.       offset = where+aSubstring.length
  78.   }
  79.  
  80.   // return it    
  81.   return count
  82. }
  83.  
  84. // Encookie -- code spaces, semicolons and commas
  85. function encookie(aString)
  86. {
  87.     var tmp = ""+aString
  88.     tmp = subst(tmp,  " ", "%20")
  89.     tmp = subst(tmp,  ",", "%2C")
  90.     tmp = subst(tmp,  ";", "%3B")
  91.     tmp = subst(tmp,  "=", "%3D")
  92.     return tmp
  93. }
  94.  
  95. // Decookie -- decode spaces, semicolons and commas
  96. function decookie(aString)
  97. {
  98.     var tmp = ""+aString
  99.     tmp = subst(tmp,  "%20", " ")
  100.     tmp = subst(tmp,  "%2C", ",")
  101.     tmp = subst(tmp,  "%3B", ";")
  102.     tmp = subst(tmp,  "%3D", "=")
  103.     return tmp
  104. }
  105.  
  106. // Search for a substring and chop before it
  107. function chopit(aString, aSubstring)
  108. {
  109.     var tmp = ""+aString
  110.     if (tmp.length == 0) return tmp
  111.     if (aSubstring.length == 0) return tmp
  112.  
  113.     var where
  114.     if ((where = tmp.indexOf(aSubstring)) < 0) return tmp
  115.     return (tmp.substring(0, where))
  116. }
  117.  
  118. // Search for a substring and lop off everything after it
  119. function lopit(aString, aSubstring)
  120. {
  121.     var tmp = ""+aString
  122.     if (tmp.length == 0) return tmp
  123.     if (aSubstring.length == 0) return tmp
  124.  
  125.     var where
  126.     if ((where = tmp.indexOf(aSubstring)) < 0) return tmp
  127.     return (tmp.substring(where+aSubstring.length, tmp.length))
  128. }
  129.  
  130.  
  131. //------------------STRING-ARRAY UTILITIES-------------------
  132.  
  133. // Find the substring at index n, counting 0 to n
  134. function doIndex(aString, n)
  135. {
  136.     
  137.     var str=""+aString
  138.     
  139.     // Count until the correct index
  140.     for(var i = 0; i < n; i++)
  141.     {
  142.         var where = str.indexOf(':', 1)
  143.         str = str.substring(where+1, str.length)
  144.     }
  145.  
  146.     // Lop off the end of the string
  147.     return str.substring(0, str.indexOf(':'))
  148. }
  149.  
  150. //------------------CUSTOM GMT UTILITY-----------------------------
  151.  
  152. // Return the 3-letter symbol for month #n
  153. function month(n)
  154. {
  155.     var m = "Jan:Feb:Mar:Apr:May:Jun:Jul:Aug:Sep:Oct:Nov:Dec:"
  156.     return doIndex(m, n)
  157. }
  158.  
  159. // Return the 3-letter symbol for day #n
  160. function day(n)
  161. {
  162.     var d = "Sun:Mon:Tue:Wed:Thu:Fri:Sat:"
  163.     return doIndex(d, n)
  164. }
  165.  
  166. // A Custom "GMT" Function
  167. function myGMT(d)
  168. {
  169.     tmp = day(d.getDay())+', '+d.getDate()+' '+month(d.getMonth())
  170.     tmp += ' 19'+d.getYear()+' '
  171.  
  172.     if (d.getHours() < 10) tmp += '0'
  173.     tmp += d.getHours()+':'
  174.     
  175.     if (d.getMinutes()< 10) tmp += '0'
  176.     tmp += d.getMinutes()+':'
  177.     
  178.     if (d.getSeconds() < 10) tmp += '0'
  179.     tmp += d.getSeconds()+" GMT"
  180.     
  181.     return tmp
  182. }
  183.  
  184. // An Alternate "GMT" Function: NOTE! This--changed for hyphens and no (19)
  185. function altGMT(d)
  186. {
  187.     tmp = day(d.getDay())+', '+d.getDate()+'-'+month(d.getMonth())
  188.     tmp += '-'+d.getYear()+' '
  189.  
  190.     if (d.getHours() < 10) tmp += '0'
  191.     tmp += d.getHours()+':'
  192.     
  193.     if (d.getMinutes()< 10) tmp += '0'
  194.     tmp += d.getMinutes()+':'
  195.     
  196.     if (d.getSeconds() < 10) tmp += '0'
  197.     tmp += d.getSeconds()
  198.     
  199.     return tmp
  200. }
  201. // --------------------COOKIE DATA UTILITIES---------------------
  202.  
  203. // Cookie Flattener
  204. function flatten()
  205. {
  206.     var tmp = ""+this.name+"="+this.value
  207.     tmp += (this.expires == null) ? "" : "; expires="+this.expires
  208.     tmp += (this.path == null) ? "" : "; path="+this.path
  209.     tmp += (this.domain == null) ? "" : "; domain="+this.domain
  210.     tmp += (this.isSecure) ? "; secure" : ""
  211.     return tmp
  212.     
  213. }
  214.  
  215. // Store Cookie in the jar
  216. function store() {document.cookie = this.flatten()}
  217.  
  218. // Set Name
  219. function setName(aName){this.name = encookie(aName)}
  220.  
  221. // Set Value
  222. function setValue(aValue){this.value = encookie(aValue)}
  223.  
  224. // Get Name
  225. function getName(aName){this.name = decookie(aName)}
  226.  
  227. // Get Value
  228. function getValue(aValue){this.value = decookie(aValue)}
  229.  
  230. // Check for WIN16 -- avoid the date bug
  231. function isWin16()
  232. {
  233.     return (navigator.userAgent.toUpperCase().indexOf('WIN16') != -1)
  234. }
  235.  
  236. // Expiration Times
  237. function setExpires(howLong)
  238. {
  239.     var now = new Date()
  240.     var epoch = isWin16() ? 0 : new Date(0)
  241.     var msecs = now.getTime()
  242.     var offset = now.getTimezoneOffset() * 60000
  243.     
  244.     var t = 0
  245.     
  246.     howLong = howLong.toLowerCase()
  247.     
  248.     // determine how long until expiration
  249.     if (howLong == 'now')    t = msecs - (3600000 * 24 * 365) // overkill
  250.     if (howLong == 'hour')   t = msecs + (3600000) // 60 minutes
  251.     if (howLong == 'day')    t = msecs + (3600000 * 24) // 24 hours
  252.     if (howLong == 'week')   t = msecs + (3600000 * 24 * 7) // 7 days
  253.     if (howLong == 'month')  t = msecs + (3600000 * 24 * 30) // 30 days
  254.     if (howLong == 'year')   t = msecs + (3600000 * 24 * 365) // 365 days
  255.  
  256.     // set the expiration
  257.     exp = isWin16() ? (t - offset) : ((t - epoch.getTime()) - offset)
  258.     now.setTime(exp)
  259.     this.expires = myGMT(now)
  260.     return this
  261. }
  262.  
  263. // Turn on security
  264. function secure(){this.isSecure = true}
  265.  
  266. // Turn off security
  267. function unsecure(){this.isSecure = false}
  268.  
  269. // get rid of a cookie right now
  270. function crumble()
  271. {
  272.     this.setExpires('now')
  273.     this.store()
  274. }
  275.  
  276. // revive a crumbled cookie
  277. function uncrumble()
  278. {
  279.     this.expires = null
  280.     this.store()
  281. }
  282.  
  283. // Cookie Constructor
  284. function JSCCookie(cname, cvalue)
  285. {
  286.     // Create and initialize cookie slots
  287.     this.name = encookie(cname)   // readable and writeable
  288.     this.value = encookie(cvalue) // readable and writeable
  289.     this.expires = null // writeable
  290.     this.isSecure = false // writeable
  291.     this.path = null    // writeable
  292.     this.domain = null  // writeable
  293.     
  294.     // Set cookie methods
  295.     this.setExpires = setExpires
  296.     this.crumble = crumble
  297.     this.uncrumble = uncrumble
  298.     this.flatten = flatten
  299.     this.store = store
  300.     this.secure = secure
  301.     this.unsecure = unsecure
  302.     
  303.     this.setName = setName
  304.     this.getName = getName
  305.     this.setValue = setValue
  306.     this.getValue = getValue
  307. }
  308.  
  309. // --------------------COOKIE JAR UTILITIES---------------------
  310.  
  311. // find a cookie by partial string
  312. function findCookie(name)
  313. {
  314.     num_in_jar = Math.min(document.cookie.length, 
  315.         count(document.cookie, ';')+1)
  316.     
  317.     if (num_in_jar != 0)
  318.     {
  319.         var tmp = ""+document.cookie
  320.         for (var i = 0; i < num_in_jar; i++)
  321.         {
  322.             var crumb = stripBlanks(chopit(tmp, ";"))
  323.             tmp = lopit(tmp, ";")
  324.             var    cname = decookie(chopit(crumb, "="))
  325.             if (cname.indexOf(name) >= 0) return crumb
  326.         }
  327.     }
  328.     return null
  329. }
  330.  
  331. // Fetch a value (by partial string)
  332. function fetch(name)
  333. {
  334.     var crumb = findCookie(name)
  335.     if (crumb == null) return null
  336.     
  337.     var    cname = decookie(chopit(crumb, "="))
  338.     var cvalue = decookie(lopit(crumb, "="))
  339.     if (cname.indexOf(name) >= 0) return cvalue
  340.     return null
  341. }
  342.  
  343. // Fetch a name (by partial string)
  344. function fetchName(name)
  345. {
  346.     var crumb = findCookie(name)
  347.     if (crumb == null) return null
  348.  
  349.     var    cname = decookie(chopit(crumb, "="))
  350.     var cvalue = decookie(lopit(crumb, "="))
  351.     if (cname.indexOf(name) >= 0) return cname
  352.     return null
  353. }
  354.  
  355. // Fetch a particular cookie (by partial string)
  356. function fetchCookie(name)
  357. {
  358.     var crumb = findCookie(name)
  359.     if (crumb == null) return null
  360.  
  361.     var    cname = decookie(chopit(crumb, "="))
  362.     var cvalue = decookie(lopit(crumb, "="))
  363.     if (cname.indexOf(name) >= 0)
  364.     {
  365.         var c = new JSCCookie(cname, cvalue)
  366.         return c
  367.     }
  368.     return null
  369. }
  370.  
  371. // --------------------COOKIE INFO UTILITIES---------------------
  372.  
  373. // Cookie Information
  374. function cookiePeek()
  375. {
  376.     size_in_jar = document.cookie.length
  377.     num_in_jar = Math.min(document.cookie.length, 
  378.         count(document.cookie, ';')+1)
  379.  
  380.     if (num_in_jar != 0)
  381.     {
  382.         document.write("<b>Current Cookies:</b> "+
  383.             document.cookie+"<p><blockquote>")
  384.         var tmp = ""+document.cookie
  385.         var crumb, cname, cvalue
  386.         for (var i = 0; i < num_in_jar; i++)
  387.         {
  388.             crumb = chopit(tmp, ";")
  389.             tmp = lopit(tmp, ";")
  390.  
  391.             cname = decookie(chopit(crumb, "="))
  392.             cvalue = decookie(lopit(crumb, "="))
  393.             
  394.             document.write("<b>Name:</b> <FONT COLOR='770000'>"+cname+"</FONT> ")
  395.             document.write("<b>Value:</b> <FONT COLOR='770000'>"+cvalue+"<br></FONT>")
  396.         }
  397.         document.write("</blockquote>")
  398.     }
  399.     else
  400.         document.write("<b>The Cookie Jar is <FONT COLOR='770000'>Empty</FONT>.</b><br>")
  401.         
  402.     document.write('There are a total of '+num_in_jar+' cookies taking up '+
  403.         size_in_jar+' bytes<p>')
  404. }
  405.  
  406. // --------------------FORMAT UTILITIES---------------------
  407.  
  408. function comment(aString)
  409. {
  410.     document.write("<FONT SIZE=4 COLOR='007777'><b>")
  411.     document.write(aString)
  412.     document.write("</b></FONT><br>")
  413. }
  414.  
  415. function observe(aString)
  416. {
  417.     document.write("<FONT SIZE=3 COLOR='007777'>")
  418.     document.write(aString)
  419.     document.write("</FONT><BR>")
  420. }
  421.  
  422. function pre()
  423. {
  424.     var l = pre.arguments.length
  425.     document.write("<PRE><FONT SIZE=3 COLOR='770000'>")
  426.         for (var i = 0; i < l; i++)
  427.         {
  428.             document.writeln(pre.arguments[i])
  429.         }
  430.     document.write("</FONT></PRE>")
  431. }
  432.  
  433.