Good morning everyone,
I am working on a legacy ASP Classic application which needs some upgrading and refactoring. One of the biggest improvements that needed to be made was to speed up the app by caching the results from database. This I have been able to do successfully by caching them to .dat files. This application follows a specific path so I have been deleting the unused cache files after I am done with them. However, on the final .asp page of the application (PV.asp), the files are getting deleted when they shouldn’t be.
My intention is this,
<input type="button" value="Done" style="width: 56px; height: 40px" onclick="finish()" />
^ this is the code for the “Done” button, it calls the finish() function. Which is below:
function finish() {
var size = "<%response.write(size) %>";
if (size == "0") {
var done = confirm("All Items mod, would you like to save?");
if (done == true)
{
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var concode = "<%=Replace(concode, "\", "\\" )%>";
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('concode').value = concode;
document.forms["confirm"].submit();
}
}
<%if NOT rsTrxTy.EOF then
if rsTrxTy.fields("TYPE").value = "TO" then%>
~Stuff happens here~
<%end if
if rsTrxTy.fields("TYPE").value = "TI" then%>
~Stuff happens here~
<%end if
end if
if NOT rsOrdrs.EOF then%>
~Stuff happens here~
<%end if %>
<%
dim fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFile "C:\M\cache\rstrxis-" & Session.SessionID & ".dat"
fso.DeleteFile "C:\M\cache\rstrxos-" & Session.SessionID & ".dat"
fso.DeleteFile "C:\M\cache\rstrxty-" & Session.SessionID & ".dat"
fso.DeleteFile "C:\M\cache\rsords-" & Session.SessionID & ".dat" %>
}
}
else {
alert("All items have not been mod.");
}
}
When the user clicks the “Done” button, the changes made to the items are committed to the database. My intention is that when this action is being performed, the cached files are deleted. However, the DeleteFile command runs even if the user doesn’t click the “done” button. It runs once the page has finished loading. This causes problems since the page will try to load from the cached file which were deleted when PV.asp was loaded. Since this app is intended to be used almost constantly without the window being closed, the session ID will remain the same which leaves not deleting the cached files out of the question (since when the app is restarted it will attempt to load what has already been cached).
My question is, why are the files being deleted when they are supposed to be contained in a javascript function that only executes when the user clicks done? I am not experienced in ASP/Javascript as this project was forced on me so I apologize if this is a simple question but I am honestly stumped.
Note- Moving to ASP.Net is out of the question. Please do not suggest this.
That’s not how ASP works. The ASP code segments (the stuff between
<% ... %>) is calculated only once when the page is first loaded.For example, if you do this:
The
fso.DeleteFilefunction invocations are called WHEN THE PAGE LOADS, not when the JavaScript function is fired.Why? When the server recieves a request for the ASP page… it first goes through and processes all the dynamic code segments between
<% %>with real HTML and then returns to the user a page that they can load. Note: A browser has no idea how to process<% %>tags! The server is the only one who can do that.Does this make sense?
You have a few options to go about what you are actually trying to do here, but I would go with performing an Ajax request to a new ASP page that all it does is call the fso.DeleteFile commands you need.
Here is an example: http://www.degraeve.com/reference/simple-ajax-example.php
Other options would be to use a get/post to the same page with a parameter that will trigger the DeleteFile. You’d have to redesign a bit to use that. I recommend the ajax solution.