Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts

    Works only first time in IE

    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.

    Code:
    		<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.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    reverse these two lines

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

    open should come first

    I highly recommend you look into prototype to do the XMLHttpRequest.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •