0

We currently have a python script that uses the Twitter API to generate a text file with a user's latest tweets.

We now need to somehow display them in a webpage, so we attempted to use an XMLHttpRequest to read the local text file and display it. Formatting is not important at the moment, we are just trying to read directly from the file.

The code we have so far, which is mostly from what we found online, is as follows:

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
    document.write("Test. ");
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  }
xmlhttp.open("GET","twitterFeed.txt",false);
xmlhttp.send();
}
</script>
</head>
<body>

<h2>Using the XMLHttpRequest object</h2>
<div id="myDiv"></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

Thanks in advance.

EDIT:

The issue we have is that it is not outputting anything, regardless of what is written in the text file. It does print out the "Test. " string that we were using to ensure that the function was being called.

As far as we can tell, there aren't any js or net errors showing up.

0

1 Answer 1

1

If you call document.write on a document in the closed state (which the document will be in as the DOM will have been completely parsed) it will implicitly call document.open which will wipe out the existing document.

You then write out Test., which you can see.

Among the things that will have been deleted will be <div id="myDiv"></div> so document.getElementById("myDiv").innerHTML=xmlhttp.responseText; will fail.

Don't use document.write.

Sign up to request clarification or add additional context in comments.

Comments

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.