Variables are printed just once!!!

Discussion in 'JavaScript and AJAX' started by pietrom, Feb 2, 2007.

  1. pietrom

    pietrom New Member

    Joined:
    Feb 2, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    I have a really stupid problem I have not been able to solve (Firefox2) :
    :confused:

    I have an xhtml page that must load an xml file with JavaScript, extract same values and print them.
    Everything goes in the right way, until I make the xhtml page print a value twice:

    HTML:
    <body onload="LoadXMLdata()">
       value = <span id="value" />            <!-- ok -->
       value = <span id="value" />            <!-- here no value is printed! -->
    </body>
    
    It must not be possible that when a variable is printed, it is automatically reset!

    :(
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Please put up the JavaScript code, otherwise it's not possible to isolate the bug.
     
  3. pietrom

    pietrom New Member

    Joined:
    Feb 2, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Here it is.

    This is the output I to get on the browser (Firefox2):

    time = 0.05
    time =

    This is the output I would like to get:

    time = 0.05
    time = 0.05

    Thank you for any information.
    OutputData.xml
    Code:
    <params>
    	<time>0.05</time>
    	<iterations>28</iterations>
    	<error>1.26</error>
    </params>
    
    printTwice.xhtml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!-- printTwice.xhtml
    	 Find out why when I print the same variable more than once, this is effectively printed the first time only.
    	 -->
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html
    	 xmlns="http://www.w3.org/1999/xhtml"
    	 xmlns:ev="http://www.w3.org/2001/xml-events"
    	 xmlns:xf="http://www.w3.org/2002/xforms"
    	 xml:lang="en" lang="en"
    	 >
    	
    	<head>
    		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    		
    		<script type="text/javascript">
    			
    			var  time = 0.0, iterations = 0, error = 0.0
    			
    			function UpdateView()
    			{
    				window.location.reload()
    			}
    			
    			function LoadXMLdata()
    			{
    				// Code for Mozilla, Firefox, Opera, etc.
    				if (document.implementation)
    				{
    					if (document.implementation.createDocument)
    					{
    						xmlDoc = document.implementation.createDocument("","",null);
    						xmlDoc.load("./outputData.xml");
    						xmlDoc.onload = GetXMLdata;
    					}
    					else
    						alert('I cannot handle this script with your browser');
    				}
    				// Code for IE
    				else if (window.ActiveXObject)
    				{
    					xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    					xmlDoc.async = false;
    					xmlDoc.load("./outputData.xml");
    					GetXMLdata();
    				}
    				else
    				{
    					alert('I cannot handle this script with your browser');
    				}
    			}
    			
    			function GetXMLdata()
    			{
    				// params { time, iterations, error }
    				document.getElementById("time").innerHTML = xmlDoc.getElementsByTagName("time")[0].childNodes[0].nodeValue;
    				document.getElementById("iterations").innerHTML = xmlDoc.getElementsByTagName("iterations")[0].childNodes[0].nodeValue;
    				document.getElementById("error").innerHTML = xmlDoc.getElementsByTagName("error")[0].childNodes[0].nodeValue;
    			}
    			
    		</script>
    		
    	</head>
    	
    	<body onload="LoadXMLdata()">
    		
    		
    		time = <span id="time" /><br />			<!-- ok -->
    		
    		time = <span id="time" /><br />			<!-- Why here the value is not printed??? -->
    		
    		
    	</body>
    	
    </html>
    
     
    Last edited by a moderator: Feb 4, 2007
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    You cannot have 1 or more elements with the same id
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Dont upload code in zipped format but keep the code in the posts so that its easy for other users to search.
     
  6. pietrom

    pietrom New Member

    Joined:
    Feb 2, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    > You cannot have 1 or more elements with the same id

    All I have to do is to print a value (obtained from a file) twice on the same web page.
    It *must* be possible somehow...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice