javascript - Deferring google charts -
i have google charts code works fine:
<script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript" async> google.load("visualization", "1", {packages:["corechart"]}); var t1 = 'indextitle' var t2 = 'indicatortitle' google.setonloadcallback(function(){drawchart1(t1)}); google.setonloadcallback(function(){drawchart2(t2)}); function drawchart1(t){ var data = new google.visualization.datatable(); data.addcolumn('string','date'); data.addcolumn('number','close'); data.addcolumn({type: 'string', role: 'tooltip'}); data.addrows([chartindexdata...........]); var options = { title: t, colors:['black'], legend: {position: 'none'}, chartarea:{left:60,top:20,width:'90%',height:'90%'} }; var chart = new google.visualization.linechart(document.getelementbyid('chart1_div')); chart.draw(data, options); } function drawchart2(t){ var data = new google.visualization.datatable(); data.addcolumn('string', 'date'); data.addcolumn('number', ''); data.addcolumn({type: 'string', role: 'tooltip'}); data.addrows([chartindicatordata...................]); var options = { title: t, legend: {position: 'none'}, chartarea:{left:60,top:20,width:'90%',height:'60%'} }; var chart = new google.visualization.linechart(document.getelementbyid('chart2_div')); chart.draw(data, options); }<% </script>
the problem when test page on google pagespeed insite score 49/100 because of script above:
remove render-blocking javascript:https://www.google.com/jsapi https://www.google.com/…ile=visualization&v=1&packages=corechart https://www.google.com/…at+en,default+en,ui+en,corechart+en.i.js optimize css delivery of following: https://www.google.com/…bdd6ab2d343a51d49841bf93d022fb/ui+en.css https://ajax.googleapis.com/…static/modules/gviz/1.0/core/tooltip.css
as understand have load javascript asynchronous or defer loading.
how fix it.
tried
window.onload = function () { google.load("visualization", "1", {packages: ["corechart"], callback: drawcharts}); var t1 = 'indextitle' var t2 = 'indicatortitle' function drawcharts() { drawchart1(t1); drawchart2(t2); } }
and worked. also, able change
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
to
<script type="text/javascript" src="https://www.google.com/jsapi" async></script>
which pushed google pageinsite score 49 82. still google pageinsight shows
optimize css delivery of following: https://www.google.com/…bdd6ab2d343a51d49841bf93d022fb/ui+en.css https://ajax.googleapis.com/…static/modules/gviz/1.0/core/tooltip.css
as understand these css loaded when
google.load("visualization", "1", {packages: ["corechart"], callback: drawcharts});
is called. how optimize that?
Comments
Post a Comment