www.webdeveloper.com
Results 1 to 4 of 4

Thread: document.writeln not working with SetTimeout

  1. #1
    Join Date
    May 2012
    Posts
    2

    document.writeln not working with SetTimeout

    Hello,
    I'm (very) new to wed developping.
    I need to write a small html page that reads an XML, and displays properties from the last tag.
    The page should refresh every 3 seconds.
    I've written this code and it's working fine expect from the refresh part.
    I tried replacing "document.writeln" with "alert" and it's working fine.

    Please help (my knowledge is quite limited, so please use simple words )

    The code:
    <html>
    <head>
    <title>EDX Status</title>
    //<META HTTP-EQUIV="refresh" CONTENT="5">
    <script type="text/javascript">
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    function readXML()
    {
    var xmlFile = "somefile.xml";
    xmlDoc.async="false";
    xmlDoc.load(xmlFile);
    xmlObj=xmlDoc.documentElement;
    var lastTag = xmlDoc.getElementsByTagName('SomeTag');
    document.writeln(lastTag[lastTag.length-1].getAttribute("AttributeOne")+" "+lastTag[lastTag.length-1].getAttribute("AttributeTwo"));
    }
    function callRead() {
    var t=setTimeout("readXML()",3000);
    }


    </script>
    </head>

    <body onload="callRead();"">

    </body>
    </html>

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    try this

    Code:
    ...
    // instead of that writeln
    document.getElementById('result').innerHTML=lastTag[lastTag.length-1].getAttribute("AttributeOne")+" "+lastTag[lastTag.length-1].getAttribute("AttributeTwo");
    ...
    HTML Code:
    <body onload="callRead();""> 
    <div id="result"></div>
     </body>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    May 2012
    Posts
    2
    it's woking great, thanks so much!

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    the problem was that you were trying to call document.write after the page is loaded. for the very first sight it seems that document.write goes in the head section but it is called by timeout when the page is already loaded
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles