Pull in Info from a HTML File using innerHTML


/ Published in: JavaScript
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.