admin管理员组

文章数量:1335371

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', '.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

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

Share Improve this question asked Jan 13, 2017 at 18:19 Ciasto piekarzCiasto piekarz 8,27720 gold badges121 silver badges221 bronze badges 1
  • Try window.onoad=function(){**Place Ajax Call Here...**} – NewToJS Commented Jan 13, 2017 at 18:25
Add a ment  | 

3 Answers 3

Reset to default 3

You will want to use the document ready statement

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

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-patible browser behaviour), but I wouldn't rely on this feature.

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');
  }
});

本文标签: javascriptrun ajax function on page loadStack Overflow