When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Wednesday, December 16, 1998

Implementing a Popup 'Tip of The Day' Window

* This project touches on the Application Object, database access, JavaScript, and disk file access.

- continued -

First, let's list the goals of this project:

    1) We would like to have a popup window with a 'Tip of the Day'.
    2) The tip displayed should automatically change each day.
    3) The tips should be stored in a database, so that people can submit tips from the Web.
    4) The tip list should be easily resettable, so as to allow the list to be recycled when there are no new entries.
    5) A complete list of tips to date should be available.

Now we'll dig into some of the details. We're only going to touch specific details here. Other information about what's happening is available by looking at the actual files supplied.

We need to know what tip is currently being displayed. In order to make transferring the current tip ID number, we'll store it in an Application variable. Also, in order to know when we need to change the tip, we will also store the date that the current tip ID number was allocated. Knowing that servers are occasionally rebooted, have power failures, etc, we need to store these values on the disk somewhere. I've decided to use the file TIPSTORE.DAT for this purpose. It contains two lines, one is the tip ID number and the other is the date.

Since these values are to be kept in Application memory all of the time, we should restore them from TIPSTORE.DAT when the application starts up. A good place to do this is in GLOBAL.ASA, in the Application_OnStart procedure. The provided GLOBAL.ASA file restores these values. This is done, courtesy of the FileSystemObject and its support through the TextStreamObject methods:

PhyPth = Server.MapPath("/tipstore.dat")
set FilSys = CreateObject("Scripting.FileSystemObject")
set TipFil = FilSys.OpenTextFile(PhyPth, 1)
Application("CurTip") = TipFil.ReadLine
Application("SymDt") = CDate(TipFil.ReadLine)
set TipFil = nothing
set FilSys = nothing

Note that we have opted to utilize Server.MapPath to tell us the physical path of the file, rather than hard coding it. This allows the same code to be used on multiple sites with no changes to the scripts that have to process the DAT file. FilSys is created as a FileSystemObject, and TipFil is created as a TextStreamObject. We then read the data from the file and close it when we're done.

Next, we should attend to the page that updates the currently selected tip. This is also probably the place where we would give the users the option of displaying the tip. I would suggest putting this on your home page, but it really could be anywhere. So let's look at this feature in HOME.ASP:


'Tip of the day processing
if Application("SymDt") < Date then
    CurTip = Application("CurTip") + 1
    SymDt = date
    Application("CurTip") = CurTip
    Application("SymDt") = SymDt

    PhyPth = Server.MapPath("/tipstore.dat")
    set FilSys = CreateObject("Scripting.FileSystemObject")
    set TipFil = FilSys.CreateTextFile(PhyPth, true)
    set TipFil = nothing
    set FilSys = nothing
end if
%> ...

So to this end, we check to see if the current date is more recent than the date we last set the tip. If were are the first person do visit this page on the new date, we become the lucky one to process the change. Locking the application, so we don't get double changes, we select the next tip ID number and store the current date so we'll have to wait for tomorrow. In keeping with the restart problem, this is also where we store the values into the DAT file.

In order to popup a browser window, we need a little help from JavaScript. Here is the function we will be using to do this:

function displayPopup(url, height, width) {

properties = "toolbar=0,location=0,scrollbars=1,height=" + = height;
properties = properties + ",width=" + width;
properties = properties + ",left=0,top=0";
popupHandle = open(url, "tipwindow", properties);

As you can see, we could select any URL we desire for this window to display. We also have control of the height and width of the window. There are some limitations to these sizes, and the location the window is placed, so check the Netscape JavaScript and MicroSoft Jscript references for the rules attendant. I've also pre-set some parameters as would be appropriate for a window such as that we want to create.

Onto the creation of the window on demand. This is achieved by using a 'javascript:' URL:

<a href="javascript:displayPopup('popup.asp',300,300)">

The 'javascript:' protocol method tells the browser that this is really a command to be interpreted by the JavaScript engine, instead of being processed by the Web Server. So we suggest to the browser that it process our function with POPUP.ASP as our URL, and a size of 300 by 300 pixels.

Perhaps we should look at POPUP.ASP at this time:


TipNbr = Application("CurTip")

set rstTip = CreateObject("ADODB.Recordset")
rstTip.Open "SELECT * FROM Tip WHERE Tip=" & TipNbr, _

We create rstTip as a Recordset to access the database, which we have conveniently created a System DSN for. The DSN I've used is named 'TipData'. When we look up the record, we specify the tip ID number we stored in Application memory.

Were we want to display information from the record we use the construct:

... <% = rstTip("TipTitle") %> ...

By this method, we read the field 'TipTitle' from the current record and return it to the browser. When we are done displaying data from the record, we close it, and release the objects as appropriate:

... <%
    set rstTip = nothing
%> ...

Included you will also find two additional ASP files. RESETTIPS.ASP forces the tip ID numbers to be reset to the beginning of the list. The other is TIPLIST.ASP, which displays all of the tips up to and including the current one in a pretty formatted list.

Happy Programming!

1 ZIP file containing the starter DAT file, the GLOBAL.ASA that restores the DAT file on application restart, and 4 ASP pages.

Related Articles
  • Everything you wanted to know about Global.asa, but were afraid to ask.
  • Automatically Configured Advertisement Display
  • Reading Windows INI Files Using ASP
  • Implementing a 'Tip of the Day' Popup Window in ASP
  • How to Ping using Active Server Pages
  • Dynamic Content Tree Creation

    Bart Silverstein has written several great articles for 4GuysFromRolla.com. When he is not writing articles, Bart is often found on the many ASP messageboards, answering questions.

    ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article