...

View Full Version : Works only first time in IE



NancyJ
05-03-2006, 11:55 PM
I cant fathom why this script only works the first time round in IE. I get no errors the second time.
The part I'm refering to is refreshForm which is set as the onchange event for each form control.



<script type = "text/javascript">

onload= function()
{
focused = "";
try
{
xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
// do non ajax here
}
registerEvents();
document.getElementById('updateButton').innerHTML = "";
}
function registerEvents()
{
if(focused != "")
{
if(document.getElementById(focused))
{
document.getElementById(focused).focus();
}

}
//obj = document.forms[0].elements;
// for(var i = 0; i<obj.length; i++)
// {
// if(obj[i].name)
// {
// if(obj[i].name.indexOf('[]'))
// {
// obj[i].onchange = refreshForm;
// if (obj[i].captureEvents) obj[i].captureEvents(Event.CHANGE);
//obj[i].onclick = function() {setFocus(this.name);}
// if (obj[i].captureEvents) obj[i].captureEvents(Event.CLICK);
// }
//
// }
// }
}
function setFocused(obj)
{
focused = obj+1;
}
function loadURL(url, args)
{
xmlhttp.onreadystatechange = handleResponse;
xmlhttp.open("GET", url+"?"+args);
xmlhttp.send(null);
}
function handleResponse()
{
if(xmlhttp.readyState == 4 &&xmlhttp.status ==200)
{
document.getElementById("quote").innerHTML = xmlhttp.responseText;
registerEvents();

}
}
function refreshForm()
{
obj = document.forms[0].elements;
var args = "";
for(var i =0; i<obj.length; i++)
{
if(obj[i].name && obj[i].name != 'getQuote')
{
args+=obj[i].name+'='+obj[i].value+'&';
}
}
loadURL('procquote.php', args);
}
</script>

It works perfectly in firefox so I have no clue why it doesnt work in ie.

I also would like to be able to determine which form control was last focused on and jump to the next one, I've got something in place but its very wonky and seems to only work sometimes, other times it picks the wrong control.

A1ien51
05-04-2006, 04:43 AM
reverse these two lines

xmlhttp.onreadystatechange = handleResponse;
xmlhttp.open("GET", url+"?"+args);

open should come first

I highly recommend you look into prototype (http://prototype.conio.net/)to do the XMLHttpRequest.

Eric



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum