/    Sign up×
Community /Pin to ProfileBookmark

Cross-browser Problem

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…
}[/CODE]

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.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceApr 08.2006 — There's a much easier solution than all of that. The XMLHttpRequest object, itself, has the ability to wait for the response.

TotalPageRequest.open("GET", "GetTotalPages.ashx", [COLOR=Red][B]false[/B][/COLOR]);

TotalPageRequest.send("");

var [COLOR=Red][B]response =[/B][/COLOR] TotalPageRequest.responseText;
Copy linkTweet thisAlerts:
@SaintJimmyauthorApr 09.2006 — I figured it would turn out to be something simple. That worked out great. Thanks!
Copy linkTweet thisAlerts:
@phpnoviceApr 09.2006 — You're welcome.

Cheers.
×

Success!

Help @SaintJimmy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...