innerHTML works in IE and Firefox, but not Chrome

前端 未结 3 1751
[愿得一人]
[愿得一人] 2021-01-18 04:09

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).



        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 04:16

    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!

提交回复
热议问题