Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html xmlns = "http://www.w3.org/1999/xhtml">
- <head>
- <title>Using Cookies</title>
- <script type = "text/javascript">
- <!--
- var now = new Date(); // current date and time
- var hour = now.getHours(); // current hour (0-23)
- var name;
- if ( hour < 12 ) // determine whether it is morning
- document.write( "<h1>Good Morning, " );
- else
- {
- hour = hour - 12; // convert from 24 hour clock to PM time
- // determine whether it is afternoon or evening
- if ( hour < 6 )
- document.write( "<h1>Good Afternoon, " );
- else
- document.write( "<h1>Good Evening, " );
- }
- // determine whether there is a cookie
- if ( document.cookie )
- {
- // convert escape characters in the cookie string to their
- // english notation
- var myCookie = unescape( document.cookie );
- // split the cookie into tokens using = as delimiter
- var cookieTokens = myCookie.split( "=" );
- // set name to the part of the cookie that follows the = sign
- name = cookieTokens[ 1 ];
- }
- else
- {
- // if there was no cookie then ask the user to input a name
- name = window.prompt( "Please enter your name", "GalAnt" );
- // escape non-alphanumeric characters in the name string
- // and add name to the cookie
- document.cookie = "name=" + escape( name );
- }
- document.writeln(
- name + ", welcome to JavaScript programming! </h1>" );
- document.writeln( "<a href= \" JavaScript:wrongPerson() \" > " +
- "Click here if you are not " + name + "</a>" );
- // reset the document's cookie if wrong person
- function wrongPerson()
- {
- // reset the cookie
- document.cookie= "name=null;" +
- " expires=Thu, 01-Jan-95 00:00:01 GMT";
- // after removing the cookie reload the page to get a new name
- location.reload();
- }
- // -->
- </script>
- </head>
- <body>
- <p>Click Refresh (or Reload) to run the script again</p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement