troubleshooting Question

Loading an XML url with javascript.

Avatar of alainbryden
alainbrydenFlag for Canada asked on
Web Languages and StandardsJavaScriptXML
30 Comments1 Solution752 ViewsLast Modified:
Hey. I'm trying to create a small script that, when my page is visited, the script will run as follows:

1. It will load an url containing XML text ( http://synthlight.miniville.fr/xml )
2. It will read in certain values and if the sum of unemployment + (100 - transport) + criminality + pollution is equal to 0
3. It will automatically redirect the page with " window.location = "http://synthlight.miniville.fr/"; "

4. If the sum of the above is not 0, it will pick the largest of unemployment, (100 - transport), criminality and pollution, and redirect to http://synthlight.miniville.fr/com, .../tra, .../sec or .../env respectively.

I have an ftp where I can upload and test the script, what I need are some people to help me pull the script together. Here is what I have attempted so far, but loading it does nothing, as the script is clearly flawed:

_______________________________________________
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
      <head>
            <META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
            <title>Synthlight City</title>
      </head>
      <body>
<script type="text/javascript">
   <!--
   var xmlDoc;
function importXML()
{
      if (document.implementation && document.implementation.createDocument)      {
            xmlDoc = document.implementation.createDocument("", "", null);
            xmlDoc.onload = createTable;
      }
      else if (window.ActiveXObject)      {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.onreadystatechange = function ()      {
                  if (xmlDoc.readyState == 4) createTable()
            };
       }
      else      {
            alert('Your browser can\'t handle this script');
            return;
      }
      xmlDoc.load("http://synthlight.miniville.fr/xml");
}
   importXML();
   var city = xmlDoc.getElementsByTagName("city");
   var unemployment = parseInt(city[0].getAttribute("unemployment"));
   var transport = parseInt(city[0].getAttribute("transport"));
   var criminality = parseInt(city[0].getAttribute("criminality"));
   var pollution = parseInt(city[0].getAttribute("pollution"));

   if (unemployment + (100 - transport) + criminality + pollution == 0)
      window.location = "http://synthlight.miniville.fr/";
   else if( (100 - transport) > unemployment && (100 - transport) > criminality && (100 - transport) > pollution )
      window.location = "http://synthlight.miniville.fr/tra";
   else if( unemployment > criminality && unemployment > pollution )
      window.location = "http://synthlight.miniville.fr/com";
   else if( criminality > pollution )
      window.location = "http://synthlight.miniville.fr/sec";
   else
      window.location = "http://synthlight.miniville.fr/env";
   //-->
</script>
You should be redirected to synthlight city automatically. If not, click <a href="http://synthlight.miniville.fr/">here</a>.
      </body>
</html>
_______________________________________________


One thing I know for sure is that xmlDoc.load("http://synthlight.miniville.fr/xml"); does not work because that particular function expects a .xml file, not a url as a parameter.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 30 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 30 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros