View Full Version : XMLHttpRequest problem

12-09-2006, 01:12 AM
Hopefully I'm posting this in the correct place. This script is a bit complex but I'm hoping there is a SCRIPT GURU out there who can help me. What I have is a JavaScript that uses a XMLHttpRequest to include info from a series of plain text files identified in an array.

What the script is supposed to do is go through the array and load the next text file at 3 second [for testing purposes] intervals. It loads the first text file just fine and the timer works. The problem is that it is not advancing through the array like it is supposed to, it just keeps reloading the first text file over and over. Please help - I'm getting a migrane.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Importing from plain text [*.txt] into HTML with XML.</title>

<script language="javascript" type="text/javascript">
//Stop the continual page loading in Firefox, Mozilla, Netscape and possibly older versions of IE.
function stopLoad(){
window.stop(); return false;
var xmlhttp=false;

//The commented out section below IS REQUIRED AS IS for IE browser engine. DO NOT uncomment it or remove it.
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
@end @*/

//The following section is for cross-browser compatability and allows the script to work in Firefox, Mozilla, Netscape, and Opera browsers.
function loadData(){
if (!xmlhttp && typeof XMLHttpRequest!='undefined'){
try {
xmlhttp = new XMLHttpRequest();
} catch (e){
if (!xmlhttp && window.createRequest){
xmlhttp = window.createRequest();
catch (e) {

//This section is the actual execution of the script which will write the contents of the *.txt file to the page.
var txtArray = ["text1","text2","text3","text4","text5","text6"];
var n = 0;
if (n == txtArray.length){return}
fileData = txtArray[n] + ".txt";
xmlhttp.open("GET", fileData, true);
if (xmlhttp.readyState==4){
document.getElementById("textDiv1").innerHTML = xmlhttp.responseText;
xmlhttp.send (http://php.net/end)(null);

<body onload="loadData();">
<div name="textDiv1" id="textDiv1"></div>

12-09-2006, 01:40 AM

You initialize n everytime you call the function

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum