I’m working on a photo browser using JavaScript’s XMLHttpRequest object to query the server for various information. I’m using ASP.NET on the server.
The one problem in particular that I’m having comes in when I query the server to get the total number of thumbnail pages for a given category. My script is set up like this:
[CODE]var TotalPageRequest;
var processingPageCount;
function GetTotalPages() {
TotalPageRequest = false;
if (window.XMLHttpRequest) {
try { TotalPageRequest = new XMLHttpRequest(); }
catch(e) { TotalPageRequest = false; }
} else if (window.ActiveXObject) {
try { TotalPageRequest = new ActiveXObject(“Msxml2.XMLHTTP”); }
catch(e) { TotalPageRequest = false; }
}
if (TotalPageRequest) {
processingPageCount = true;
TotalPageRequest.onreadystatechange = ProcessTotalPageRequest;
TotalPageRequest.open(“GET”, “GetTotalPages.ashx”, true);
TotalPageRequest.send(“”);
}
}
function ProcessTotalPageRequest() {
if (TotalPageRequest.readyState == 4) {
if (TotalPageRequest.status == 200) {
// Display total pages on the navigation bar
var ttlPagesLabel = document.getElementById(“lblTotalPages”);
ttlPagesLabel.innerHTML = TotalPageRequest.responseText;
lastPage = parseInt(TotalPageRequest.responseText);
processingPageCount = false;
} else {
alert(TotalPageRequest.statusText);
}
}
}
function gotoPage(pg) {
processingPageCount = false;
GetTotalPages();
var ExtApplet = document.getElementById(“ExtensionApplet”);
while (processingPageCount) { ExtApplet.Pause(1.5); }
// Should have a valid page count at this point… continue execution
// Additional code omitted…
}
And now for a little explanation… You can see I’ve got some module-level variables up there for the request object (TotalPageRequest) and one to flag the processing status (processingPageCount) using a boolean value.
The gotoPage function is called as soon as the page loads with a 1 as the only parameter to call up the first page. The function makes a call to GetTotalPages to query the server and return the total number of pages. But I need to pause after that call to allow the value to return before executing the rest of the function where I proceed to validate the pg parameter along with a few other tasks including another XMLHttpRequest to retrieve the HTML for the first page of thumbnails.
Since JavaScript doesn’t have a good method of pausing execution, I wrote a small utility Applet that contains a public Pause method that takes a time in seconds and halts execution for the specified time before returning. This seems to be working much better than just leaving the while() loop empty and testing the flag about a million times a second.
Now the problem:
This script works beautifully in IE. But when I try to view the page in Netscape, the JavaScript console is telling me that the TotalPageRequest and processingPageCount variables are not defined. And the while() loop executes infinitely… locking up the browser.
Do you see anything in this script that would explain that behavior?
Thanks in advance for any advice.