Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

innerHTML works in IE and Firefox, but not Chrome

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.

like image 347
user1114060 Avatar asked Dec 23 '11 22:12

user1114060


People also ask

Why innerHTML does not work?

People can struggle and complain about innerHTML not working. Such things usually occur because of human error, when strings are not appropriately defined, or there are some mistakes in JavaScript code.

Does Firefox support innerText?

javascript - 'innerText' works in IE, but not in Firefox - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

What attribute replaces innerHTML?

According to the official documentation, dangerouslySetInnerHTML is React's replacement for using innerHTML in the browser DOM. This means that if in React if you have to set HTML programmatically or from an external source, you would have to use dangerouslySetInnerHTML instead of traditional innerHTML in Javascript.

What is the difference between HTML and innerHTML?

It then adds them to the DOM separately in a manner that causes their execution. .html() implicitly causes a few operations (script handling being one) whereas writing to innerHTML simply causes the innerHTML to change, but very little is done with that HTML.


1 Answers

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!

like image 55
Sergio Tulentsev Avatar answered Oct 07 '22 01:10

Sergio Tulentsev