Reputation: 45
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.
Upvotes: 0
Views: 490
Reputation: 943108
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
.
Upvotes: 1