home *** CD-ROM | disk | FTP | other *** search
/ MacHome 2001 June / MacHome Magazine Demo Disc June 2001.iso / Stuff / Software / Graphic / PageSpinner 3.0.2 / Examples / JavaScript / Dynamic Document Stationery < prev    next >
Encoding:
Text File  |  2000-10-16  |  5.1 KB  |  189 lines  |  [TEXT/JyWs]

  1. <html>
  2. <head>
  3. <title>Dynamic Document</title>
  4.  
  5. <script language="JavaScript" type="text/javascript">
  6. <!--
  7. /*
  8.     Dynamic Document
  9.     Written by Jerry Aman, Optima System, Aug 2, 1996. 
  10.     Part of the PageSpinner distribution.
  11.  
  12.     Modified October, 2000
  13.  
  14.     We will not be held responsible for any unwanted 
  15.     effects due to the usage of this script or any derivative.  
  16.     No warrantees for usability for any specific application are 
  17.     given or implied.
  18.  
  19.     You are free to use and modify this script,
  20.     if the credits above are given in the source code
  21. */
  22.  
  23. // getHourOfDay()
  24. // Returns: Integer containing the hour 0-24
  25.  
  26. function getHourOfDay()
  27. {       var now = new Date();
  28.         return(now.getHours());
  29. }
  30.  
  31.  
  32. // getTime()
  33. // Returns: Text containing the time in the format HH:MM
  34.  
  35. function getTime()
  36. {       
  37.     var now = new Date();
  38.     var minutes = now.getMinutes();
  39.     var divider = ":";
  40.  
  41.     if (minutes<10)
  42.         divider = ":0";
  43.  
  44.         // Hack to get it to display the time 
  45.         // correctly in version 3.0, (adjust for offset)
  46.     if (navigator.appVersion.lastIndexOf('3.') != -1 && 
  47.         navigator.appName.lastIndexOf('Netscape') != -1)
  48.             return( now.getHours()-1 + divider + minutes );
  49.  
  50.         // Other versions may work with this ?    
  51.     return( now.getHours() + divider + minutes );
  52. }
  53.  
  54.  
  55. // isMacintoshBrowser()
  56. // Returns: Boolean value, true if the browser is running under MacOS
  57.  
  58. function isMacintoshBrowser()
  59.     return(navigator.appVersion.lastIndexOf('Mac') != -1 );
  60. }
  61.  
  62.  
  63. // sayHello ()
  64. // Inserts text dynamically in the document when it is called
  65. // Note that all text are dynamically inserted into the document
  66. // when a call to  this function are made in the BODY part of the file.
  67.  
  68. function sayHello ()
  69. {
  70.     document.write(    "I see that the time is <B>" + getTime() + "</B>, so I wish you" );
  71.  
  72.     if(getHourOfDay()<5 || getHourOfDay()>19)
  73.         document.write('<FONT COLOR="2C396D"> a good night!</FONT>');
  74.     else 
  75.     {
  76.         if ( getHourOfDay() <11) 
  77.         {
  78.             document.write('<FONT COLOR="52A553"> a good morning!</FONT>');
  79.         } 
  80.         else 
  81.         { 
  82.             document.write('<FONT COLOR="ED363C"> a good day!</FONT>');
  83.         }
  84.     }
  85. }
  86.  
  87. // -->
  88. </script>
  89.  
  90. </head>
  91. <body bgcolor=FFFFFF text=000000>
  92. <h1>JavaScript Dynamic Document</h1>
  93.  
  94. <p><b>This stationery page contains JavaScript that creates a dynamic page</b></p>
  95.  
  96. <p>Here is an example on how JavaScript can be used to provide a dynamic page that will change its contents depending upon the local time specified in the browser's system and the OS that is used by the reader.</p>
  97.  
  98.  
  99. <!-- 1) Let the reader know the browser ID used
  100.       (this is often logged by servers for keeping statistics) -->
  101.  
  102. <p>Welcome <b><script language="JavaScript" type="text/javascript">
  103. <!--
  104. document.write(navigator.userAgent)
  105. // -->
  106. </script></b>!</p>
  107.  
  108. <!-- 2) Tell the name of the browser used -->
  109.  
  110. <script language="JavaScript" type="text/javascript">
  111. <!--
  112. document.write("Nice to see that you use ", navigator.appName," ", navigator.appVersion, ".")
  113. // -->
  114. </script>
  115. <p>
  116.  
  117.  
  118. <!-- 3) Say something that depends upon the readers local time -->
  119. <!-- This is all done in the function sayHello -->
  120.  
  121. <script language="JavaScript" type="text/javascript">
  122. <!--
  123. sayHello()
  124. // -->
  125. </script>
  126.  
  127.  
  128. <!-- 4) Finally, say something special to the Mac OS users -->
  129.  
  130. <script language="JavaScript" type="text/javascript">
  131. <!--
  132.  
  133. if (isMacintoshBrowser())
  134.     document.write(
  135.     '<P><FONT COLOR="385FD1">Very, very nice to see that you are using MacOS!</FONT>'); 
  136. else
  137.     document.write(
  138.     '<P>Sad too see that you are not using a Mac to view this page.')
  139. // -->
  140. </script>
  141.  
  142. <hr>
  143. <b>How to use:</b><br>
  144. <p>Four small scripts are embedded inside the text contents of this file. The scripts are executed - and inserts text - when the page is being loaded. View the source file to see how this is done. Every function is commented.</p>
  145.  
  146. <p>
  147. Replace the contents inside the script with calls to your own test functions and add text you want to have displayed inside the <font color="FF3366"><code>document.write()</code></font> function in the script. It is recommended to create functions  for complex scripts and place these in the HEAD section, see the <tt>sayHello</tt> function for an example of this. </p>
  148.  
  149. <p>
  150. Make sure that all text is placed inside the <font color="FF3366"><code>document.write()</code></font> function, otherwise it will not look good on browsers that don't support JavaScript.</p>
  151.  
  152. <p>
  153. Edit this page or copy selected scripts to create your own dynamic page!</p>
  154.  
  155. <hr>
  156. <p>
  157. <b>Example code for the first dynamic text above:</b>
  158. <pre>Welcome <B><SCRIPT LANGUAGE="JavaScript">
  159. <!--
  160. document.write(navigator.userAgent)
  161. // -->
  162. </SCRIPT></B>!
  163. </pre>
  164.  
  165. <p>Note that the text "<b>Welcome !</b>" is the only text that will be displayed in browsers that don't support JavaScript.</p>
  166.  
  167. <hr>
  168. <p>
  169. <b>This function located in the HEAD section returns true if the browser is running on a Mac:</b></p>
  170.  
  171. <pre>function isMacintoshBrowser()
  172.   return(navigator.appVersion.lastIndexOf('Mac') != -1 );
  173. }
  174. </pre>
  175.  
  176. <p>The function can be used inside a script like this:</p>
  177.  
  178. <pre>if (isMacintoshBrowser())
  179.   document.write('Mac specific text...'); 
  180. else
  181.   document.write('Text for other platforms...')
  182. </pre>
  183.  
  184.  
  185. </body>
  186. </html>
  187.