Hi,

I managed to get this function in a ajax script working for all browsers except IE!

function ShowPrevSlides(selcat,selpic,catno) {
	objXmlHttp=GetXmlHttpObject(PrevMenuResponse);
	objXmlHttp.open("GET",url1 + "?id=" + selcat);
	objXmlHttp.selcat = selcat;
	objXmlHttp.selpic = selpic;
	objXmlHttp.catnr = catno;
	objXmlHttp.send(null);
}

The problem is that I need those variables to be send in this response function, n.l. PrevSlide():

function PrevMenuResponse() {
	if (objXmlHttp.readyState == 4) {
		if (objXmlHttp.status == 200) {
			document.getElementById("menu-design").innerHTML='';
			document.getElementById("menu-design").innerHTML=objXmlHttp.responseText;
			PrevSlide(objXmlHttp.selcat,objXmlHttp.selpic,objXmlHttp.catnr)
        }
	} else {return}
}

It might be I'm using a bug in my advantage but it realy works on FF (mac and windows) and Safari (mac) but it wont work in IE 6 or IE 7

Is there any other way to do this?

Thanks in advance
Niels

Not knowing exactly what your other functions do, you could try sending the values along as parameters to the callback function.

Something like:

var ajaxObj;
function AjaxFunction(par1, par2) {
  callback = function(){ AjaxCallback(par1, par2); };
  ajaxObj = GetXmlHttpObject(callback);
  ajaxObj.open("GET", "http://example.com");
  ajaxObj.send(null);
}

function AjaxCallback(par1, par2) {
  if(ajaxObj.readyState == 4) {
    doStuff(par1, par2);
  }
}

Hi Atli,

First, thanks for your reply.
I've tried to do something like you mentioned but i only gert errors in my script.
I must do something wrong or it doesn't work.

I will explain a littlebit more.
With the script I make an AJAX request for getting some picture of some category out of an array. After 3 secs it automaticly does it again for the next picture out of the array.

So in my callback function the img tag gets a new url for the picture after readystate 4 and after that it needs to calculate the next picture out of the array. For this my script needs to know what id, category, and so on we are, so I need to pass those vars with it.

In this line

objXmlHttp=GetXmlHttpObject(PrevMenuResponse);

I can not give those parameters.
I liked your way and it looks like it should work but I cant get it working.

Maybe I'm almost there
Thanks, Niels

I did a little test myself, maybe it can help you.

It simply sent a request to a PHP script to update a couple of numbers, which I then displayed and updated ever 1 second.

This is the HTML

<html>
<head>
	<title>Test</title>
	<script type="text/javascript" src="script.js"></script>
</head>
<body onload="javascript: SendRequest(0, 1);">
	<div id="Output">Loading...</div>
</body>
</html>

And the "script.js" file

var ajaxObj;

/** Create a new XmlHttpRequest **/
function GetXmlHttpObject(pCallback) {
	request = null;
	if(window.XMLHttpRequest) { // Standard method
		request = new XMLHttpRequest();
	}
	else { // IE crap
		try {
			request=new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				request=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
				return false;
			}
		}
	}
	request.onreadystatechange = pCallback;
	return request;
}

/** Send the request **/
function SendRequest(par1, par2) {
	callback = function(){ AjaxCallback(par1, par2); };
	ajaxObj = GetXmlHttpObject(callback);
	ajaxObj.open("GET", "getIndex.php?first="+ par1 +"&second="+ par2);
	ajaxObj.send(null);
}

/** Request callback **/
function AjaxCallback(par1, par2) {
	if(ajaxObj.readyState == 4) {
		UseData(ajaxObj.responseText);
	}
}

/** Parse the input data and start again **/
function UseData(pInput) {
	pieces = pInput.split(":");
	first = parseInt(pieces[0]);
	second = parseInt(pieces[1]);

	document.getElementById('Output').innerHTML = "First: "+ first +" - Second: "+ second;

	timeout = function(){ SendRequest(first, second); };
	setTimeout(timeout, 1000);
}

And the PHP:

<?php
	($first = @$_GET['first']) or $first = 0;
	($second = @$_GET['second']) or $second = 0;
	$first++; $second++;
	echo "$first:$second";
?>

Worked perfectly in all browsers I tested in (including IE7).

Hi Atli,

Thank you so much,

Ive been folowing your advice and tried again.
I mannaged to get it working!

I did not check on windows IE since im not in the office but I'm hopefull that it works now with you method!

I had this problem before and now I know how to solve it next time
Thanks

Niels

ps the result of my work http://www.tackenco.nl/?id=177

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.