|
|
Keeping Count of Downloads
You are here: irt.org | Articles | JavaScript | Miscellaneous | Keeping Count of Downloads Published on: Saturday 18th April 1998 By: Martin Webb IntroductionHave you got files on your web space that people can download? Do you want to count how many times they are downloaded? This article discusses how to keep count of how many times a file is downloaded. Using a combination of image or floating frame replacement, this utility loads a CGI image to increment a counter. You obviously need to have a CGI image counter for this to work - most people do - either from your ISP or from a third party. Zip download filesThe first question that needs answering is: How do can someone download a file from a web site? Using a normal link it is possible to download for example a zip file. By default the browser does not know how to handle a zip file, so it asks the user what it should do with it, so it prompts the user whether it should open it or save it to disk. Saving it to disk is the desired response. Rather than have a link that connects directly to the zip file, e.g.:
we can use the JavaScript protocol to capture the link selection and perform some other action, e.g.:
The only problem with this approach is that browsers allow a shift-click or a right mouse button click on the link, which allows the visitor to save the file linked to. In our case this would case a problem because there is not a file pointed at by the link. A solution is to replace the link with a form button:
Image CountersMost ISP's provide a basic CGI script that allows a counter to count how many times a page is visited. At the same time they generally provide an image of the counter so that you or your visitor can view the count. There are one or two of these such image counters on my pages, but you will not see them as they are hidden. Generally a CGI Image Counter looks similar to:
What this does, is instead of loading an image, it runs the CGI script at the server which then returns a variable image which is displayed on the screen. Obviously the implementation of your CGI Image Counter will be different to that shown above. To hide the returned image is very simple indeed:
This still invokes the CGI script which returns an image, but its size is set very small indeed, 1 pixel wide by 1 pixel high. About the size of a full stop. Image SwappingOkay then, we've got our basic image, now whats to stop us from using some good old image swapping code to change from one image to another? Nothing, except the fact that some older browsers do not allow image swapping - more on that in the next section. If we start with a normal blank image:
And then name it:
We can now use JavaScript to swap the image from the normal blank image to an image returned by a CGI Script:
Floating Frame SwappingI mentioned in the previous section that not all browsers support image swapping. This is because they did not implement the JavaScript Image object. We can easily detect whether the browser supports the Image object with the following simple check:
So what can we do with browsers that do not support the Image object? Wel for Microsoft Internet Explorer 3, we can utilise floating frames. We can load an initial floating frame into the document containing just a blank image, and then, using JavaScript, we can change the contents of the floating frame so that it has the image returned by a CGI Script. First the HTML code to display a hidden floating frame:
The initial source contained within the floating frame (count0.html) looks like:
The JavaScript code required to change the location of the floating frame looks like:
Where the contents of count1.html looks like:
So we now have the ability to swap images on MSIE3. Unfortunately, Netscape Navigator 2 does not support the Image object or floating frames, so we can't change an image to an Image Counter on demand, but, we can at least make sure it doesn't cause any problems. The SolutionWe can bring together all the techniques described above to produce a utility that counts the number of times a form button is pressed and therefore the number of times a file download is requested. First the JavaScript code to load either the hidden blank image, or the hidden floating frame, and display the form download button:
Second, the initial contents (count0.html) of the floating frame:
Third, the reloaded floating frames contents (count1.html):
View the profile on Martin Webb and the list of other Articles by Martin Webb. |
-- div -->
|