Ciasto piekarz
Ciasto piekarz

Reputation: 8277

run ajax function on page load

Hi I am trying to load content and dump json on page: index.html

<html>
  <head>
  <title>Learning</title>
  <style type="text/css">
  body { background-color: #ddd; }
  #container { height: 100%; width: 100%; display: table; }
  #inner { vertical-align: middle; display: table-cell; }
  #gauge_div { width: 120px; margin: 0 auto; }
</style>
  </head>
  <body id="body">
    <script src="main.js"></script>
    <div id="animal-info"></div>
  </body>
</html>

main.js

var animalContainer = document.getElementById("animal-info");

var ourRequeast = new XMLHttpRequest();
var loaded = document.getElementById("body");

body.addEventListener("onload", function(){


ourRequeast.open('GET', 'https://learnwebcode.github.io/json-example/animals-1.json');
ourRequeast.onload = function() {
// console.log(ourRequeast.responseText);
// var ourData = ourRequeast.responseText;
var ourData = JSON.parse(ourRequeast.responseText);
// console.log(ourData[0])

renderHTML(ourData);
}
ourRequeast.send();
});

function renderHTML(data){
    animalContainer.insertAdjacentHTML('beforeend', 'testing 123');
}

so I am hoping that it should print "testing 123" on page load but its neither giving error nor showing anything

Upvotes: 2

Views: 12751

Answers (3)

Jeoff
Jeoff

Reputation: 99

You will want to use the document ready statement

$(document).ready(function(){
    console.log('this is run on page load');
    myFunction();
});

Upvotes: 3

Dan_
Dan_

Reputation: 1245

Best solution is to wrap your whole code in the following;

document.addEventListener("DOMContentLoaded", function(event) { 
  //do stuff here
});

This will execute all of the code when the DOM has finished loading. This is supported by pretty much all browsers but those not really used anymore, like IE8.

You can also do this just for calling the function, but some variables that get DOM elements might not grab the nodes properly if they are yet to load.

Complete JS for example, also tested in JSFiddle. Works fine.

document.addEventListener("DOMContentLoaded", function(event) { 
  var animalContainer = document.getElementById("animal-info");

  var ourRequeast = new XMLHttpRequest();
  var loaded = document.getElementById("body");

  ourRequeast.open('GET', 'https://learnwebcode.github.io/json-example/animals-1.json');
    ourRequeast.onload = function() {
    // console.log(ourRequeast.responseText);
    // var ourData = ourRequeast.responseText;
    var ourData = JSON.parse(ourRequeast.responseText);
    //console.log(ourData[0])

    renderHTML(ourData);
  }
  ourRequeast.send();

  function renderHTML(data){
      animalContainer.insertAdjacentHTML('beforeend', 'testing 123');
  }
});

Upvotes: 0

Rafael
Rafael

Reputation: 18522

Event types passed to addEventListener are expected to be without the on prefix. Instead of addEventListener("onload", function () { use addEventListener("load", function () {.

Also, you're attaching event listener to variable body, but this variable is not defined in the code. It may still work, if there is an element with id="body" in the document (backward-compatible browser behaviour), but I wouldn't rely on this feature.

Upvotes: 0

Related Questions