First Time Only

When this page finished loading in your browser you should see an alert pop up with "Welcome" in it. At least that is what you should see the first time that you visit this page. If you have visited the page before during your current browser session then you will not see that alert.

That alert is just an example. The purpose of this script is to determine whether this is the first time that a web page has been accessed in this browser on this computer and if it is to perform a given piece of processing (in the example on this page it calls the alert with the welcome message).

How the script works is that it tests for the existance of a particular cookie and then checking if that cookie contains text that matches the filename of the current page (without the extension). If the appropriate cookie exists and has that content then the script assumes that you have visited the page before.

Because the script uses session cookies nothing is actually written to your visitor's computer and the cookie will be automatically removed when they close their browser.

To add first time only processing to your web page you first need to copy the following Javascript code into the head section of your page.

Copy the above Javascript and save it to an external file. I called mine firsttime.js.

We can now link the Javascript into the head of our web page using the following code:

<script type="text/javascript" src="firsttime.js"></script>

The final step is to replace the alert('welcome'); statement with whatever processing it is that you want to have performed only on the first visit to the page.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow