0

I couldn't get Javascript to list "start" numbers on website because it empty.

Here my html body codes,

<script type="text/javascript">
    captionsDoc = loadXMLDoc("captions.xml");
    x=captionsDoc.getElementsByTagName('text');

    for(i=0;i<x.length;i++)
    {
        document.write(x[i].getAttribute('start'));
        document.write("/n");

    }
</script>

then this is sample of xml codes from captions.xml

<transcript>
<text start="6.738" dur="2.277">and explain a few interesting points about them.</text>
<text start="9.016" dur="2.722">But first I need to crush your expectations.</text>
<text start="24.716" dur="1.611">So let&#39;s begin.</text>
<text start="26.328" dur="2.535">First we start with the solvent diethyl phthalate.</text></transcript>
10
  • document.wite <-- did you miss this? Commented Mar 13, 2013 at 19:16
  • 1
    Also, don't use document.write, update the innerHTML of an element on the page. Commented Mar 13, 2013 at 19:17
  • I misstype it, it still doesn't work with corrected one. Commented Mar 13, 2013 at 19:21
  • By "doesn't work" what do you mean? Does it loop? Do you get a JS error in the console? Commented Mar 13, 2013 at 19:22
  • I fixed it and it doesn't show any error on JS console on Firefox FireBug console. The site still empty Commented Mar 13, 2013 at 19:44

1 Answer 1

1

you have

document.wite("
")

you should have

document.write("\n");

Then it works with a charm here, with load function from: http://www.w3schools.com/dom/dom_loadxmldoc.asp

LIKE THIS:

<html>
    <head>
        <title>HTML5 included Javascript....</title>
        <meta name="description" content="Test" charset="utf-8"></meta>
        <script type="text/javascript">
            function loadXMLDoc(dname)
            {
                if (window.XMLHttpRequest)
                {
                xhttp=new XMLHttpRequest();
                }
                else
                {
                xhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xhttp.open("GET",dname,false);
                xhttp.send();

                return xhttp.responseXML;
            }

            function init()
            {
                var c = document.getElementById('container');
                captionsDoc = loadXMLDoc("captions.xml");
                x=captionsDoc.getElementsByTagName('text');

                for(i=0;i<x.length;i++)
                {
                    c.innerHTML += x[i].getAttribute('start');
                    c.innerHTML += "\n";
                }
            }

            window.onload = init;
        </script>
    </head>

    <body>
        <div id="container">
        </div>
    </body>
</html> 
Sign up to request clarification or add additional context in comments.

1 Comment

That's weird... But you should be sure the whole document is loaded before trying to write too it. And it's preferable to modify the DOM-structure over using document.write(). I've edited mt answer with what works here. (Also that document.write that i initially replaced threw an error for me)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.