onreadystatechange problem

N

noon

This code is partially pseudo just to simplify and get my point
across. I'm searching IMDB for a movie title. Depending on the search
results, sometimes IMDB pushes you directly to a movie's info page and
other times it brings you to a search results list. I'm determining
which page I am at, and if at the search results page making another
request and overwriting the html for processing of the movie's info
page. But because of onreadystatechange my variable contents of html
aren't being overwritten in time.

At first was wondering if there was a way to add an event listener to
my second XMLHttpRequest so I can see when it loads, but that wouldn't
work because I don't always need to do this extra processing if I am
at the movie info page.

Any suggestions? :(

(...)
req.onreadystatechange = checkPage;
(...)

function checkPage(html) {
var title = 'Initial title';

// Are we at the search results page
// or did we go directly to the movie info page?
if (html.indexOf('<title>Search results') != -1) {
// Strip out the link
var link = html.substring(s, html.indexOf('</a>', s));
link = link.substring(link.indexOf('<a href="')+9,
link.lastIndexOf('">'));

// Make another XMLHttpRequest with this new link
// so that we are on the title page and can begin
var xhr = new XMLHttpRequest();
xhr.open("GET", 'http://www.imdb.com' + link, true);
xhr.onreadystatechange = function() {
// Check if ready
if (xhr.readyState != 4) return;
if (xhr.status != 200) return;

// Overwrite the html
html = xhr.responseText;
title='uh';
alert(title);
}
xhr.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
xhr.send(null);
}
alert("The title = " + title);

// Do processing on movie info page here...

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top