Hi there,

I have a problem in FireFox, whereby a Javascript function only works if I make the first line of code an alert() statement.
If I take out the alert() statement, the following code doesn't work. Any ideas ?

The alert() statement I am talking about, is the first line of the setupPage() function

Many thanks in advance,
James

<script type="text/javascript">
var xmlDoc;
function loadXML()
{
//load xml file
// code for IE
if (window.ActiveXObject)
{
	xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async=false;
	xmlDoc.load("master.xml");
	setupPage();
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
	alert('start Firefox code');
	xmlDoc=document.implementation.createDocument("","",null);
	xmlDoc.load("master.xml");
	xmlDoc.onload=setupPage();
//	alert('end Firefox code');
}
else
{
	alert('Your browser cannot handle this script');
}
}
function setupPage()
{
alert('setuppage code');  <----- this is the alert statement that makes the code work
var x;
var y;
var z;
x = xmlDoc.getElementsByTagName("lead_recipient")[0].childNodes[0].nodeValue;
y = replaceChars(x);
document.myForm.recipient.value = y;
document.myForm.courtesy_our_email.value = y;

etc etc

Im not sure if this is what you need!
But both condition can be achieved with a try...catch block.

<html>
<head>
<title>DEMO</title>
<script type="text/javascript">
<!--
var xmlDoc;

var setupPage = function() {
var x, y, z;
try { // lead_recepient
   x = xmlDoc.getElementsByTagName("lead_recipient")[0].childNodes[0].nodeValue;
  y = replaceChars( x ); // Am a bit confused on these declaration !?
  document.myForm.recipient.value = y;
} // If this one fails to execute - then the statement below will be executed. 
// which ever comes first

catch( error ) { 
  x = xmlDoc.responseXML.getElementsByTagName("lead_recipient")[0].childNodes[0].nodeValue; 

if ( xmlDoc.readyState !== 4 ) return;
if ( xmlDoc.status !== 200 ) {
   return alert("Unable to parse XML data!"); }

y = x.replace(/\s/g, "-"); // Demostration sample, no need to apply these.

document.myForm.recipient.value = y;
  }    alert( "setuppage code" );
};

var loadXML = function( url ) {
xmlDoc = null;
   if ( window.XMLHttpRequest ) {
   try {
      if ( document.implementation.createDocument ) {
      alert("start Firefox code");
      xmlDoc = document.implementation.createDocument("", "", null);
      xmlDoc.load( url ); 
     }
   }
   catch( e ) {
      xmlDoc = new XMLHttpRequest();
     }
   } 
   else if ( window.ActiveXObject ) {
      try {
      xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch( er ) {
      xmlDoc = new ActiveXObject("Msxml2.XMLHTTP");
      }
   }
   if ( xmlDoc !== null ) {     
  xmlDoc.onreadystatechange = setupPage; 
     xmlDoc = ( xmlDoc.async ) ? xmlDoc.async = false : xmlDoc;
     xmlDoc.open("GET", url, true);
     xmlDoc.send( null );      
  } else { alert("Your browser cannot handle this script"); 
  }
};
window.onload = loadXML("master.xml");
// -->
</script>
</head>
<body>
<form id="myForm" name="myForm" action="#" onsubmit="return false;">
<div>

<input type="text" id="recipient" name="recipient" size="20" value="" />
</div>
</form>
</body>
</html>
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.