Reading from text file with HTML and Javascript -
we have python script uses twitter api generate text file user's latest tweets.
we need somehow display them in webpage, attempted use xmlhttprequest read local text file , display it. formatting not important @ moment, trying read directly file.
the code have far, found online, follows:
<!doctype html> <html> <head> <script> function loadxmldoc() { if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code 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 xmlhttprequest object</h2> <div id="mydiv"></div> <button type="button" onclick="loadxmldoc()">change content</button> </body> </html>
thanks in advance.
edit:
the issue have is not outputting anything, regardless of written in text file. print out "test. " string using ensure function being called.
as far can tell, there aren't js or net errors showing up.
if call document.write
on document in closed state (which document in dom have been parsed) implicitly call document.open
wipe out existing document.
you write out test., can see.
among things have been deleted <div id="mydiv"></div>
document.getelementbyid("mydiv").innerhtml=xmlhttp.responsetext;
fail.
don't use document.write
.
Comments
Post a Comment