We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

1man on 03/14/07


Tagged

ajax file html DOM innerHTML


Versions (?)


Who likes this?

4 people have marked this snippet as a favorite

kyokutyo
DataSquirrel
d4rk
vali29


Pull in Info from a HTML File using innerHTML


Published in: JavaScript 


Simply replaces the div's innerHTML with the response text received from the file.

  1. /*Executed on click. Passes the url to the function. Function opens the URL then
  2.  *the parseResponse function is called
  3.  */
  4. function grabFile(file) {
  5. var request = getHTTPObject();
  6. request.onreadystatechange = function() {
  7. parseResponse(request);//this is what happens once complete
  8. }
  9. request.open("GET",file,true);
  10. request.send(null);
  11. }
  12. /*Once the request state is complete and the file exists, it grabs the results
  13.  * div, and inserts the response text and innerHTML.
  14.  */
  15. function parseResponse(request) {
  16. if(request.readyState == 4){
  17. if(request.status == 200 || request.status == 304){
  18. var results = document.getElementById("results");
  19. results.innerHTML = request.responseText;
  20. } else {
  21. alert("Something Broke!");
  22. }
  23. }
  24. }

Report this snippet 

You need to login to post a comment.