innerHTML works in IE and Firefox, but not Chrome
Asked Answered
A

3

7

The data will not display in Chrome, unless i open an IE tab in Chrome go to the site then close it back to Chrome (sorry, if that doesn't make much sense).

window.onload = function() {
    var url = "http://----.freeiz.com/gbSales/sales.json";
    var request = new XMLHttpRequest();
    request.open("GET", url);
    request.onload = function () {
        if (request.status == 200) {
            updateSales(request.responseText);
        }
    };
    request.send(null);
}
function updateSales(responseText) { 
    var salesDiv = document.getElementById("sales");
    salesDiv.innerHTML = responseText;
}

Im just starting to learn JavaScript so I really don't know much about it.

Apopemptic answered 23/12, 2011 at 22:44 Comment(0)
U
9

You should use some modern Javascript library. It guards you from many of those small differences between browsers. I like jQuery.

So, with jquery your code

window.onload = function() {
  var url = "http://----.freeiz.com/gbSales/sales.json";
  var request = new XMLHttpRequest();
  request.open("GET", url);
  request.onload = function () {
    if (request.status == 200) {
      updateSales(request.responseText);
    }
  };
  request.send(null);
}
function updateSales(responseText) { 
  var salesDiv = document.getElementById("sales");
  salesDiv.innerHTML = responseText;
}

becomes

$(document).load(function() {
  var url = "http://----.freeiz.com/gbSales/sales.json";

  $.get(url, {}, function(data) {
    $('#sales').html(data);
  });
});

Shorter, cleaner and works in all browsers!

Ungracious answered 23/12, 2011 at 22:47 Comment(1)
by the way, if a question works for you, you should accept it (that check mark on the left)Ungracious
H
0

I think you want to use:

request.onreadystatechange = function() {

instead of:

request.onload = function() {

And change the way you check the return value.

See the asynchronous request code example here: https://developer.mozilla.org/en/DOM/XMLHttpRequest/Using_XMLHttpRequest for more details.

Hillard answered 23/12, 2011 at 23:7 Comment(0)
L
0

Just find that only the first form tag is removed so you can put an empty form () and the next one is keep in the code.

Laurielaurier answered 15/5, 2013 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.