我正試圖在不同的選項卡上創建一個高圖。但林得到錯誤,高圖未定義。我試圖用不同的方法註冊Highchart.js。代碼如下。爲document.createElement('script')設置'src'屬性?
function OpenWin() {
var w = window.open();
w.document.open();
w.document.write('<div id="container" style="width:100%; height:400px;"></div>');
var scriptHead = w.document.createElement("SCRIPT");
//scriptHead.setAttribute('src','http://code.highcharts.com/highcharts.js');
var link = "http://code.highcharts.com/highcharts.js";
scriptHead.src = link;
w.document.head.appendChild(scriptHead);
var script = w.document.createElement("SCRIPT");
w.document.body.appendChild(script);
var js = w.document.createTextNode('var a = localStorage.getItem("ImportOptions"); console.log(JSON.parse(a)); var chart = new Highcharts.Chart(JSON.parse(a))');
script.appendChild(js);
w.document.close();
}
您應該使用腳本加載事件來定義使用它的邏輯。我猜是你添加它的方式,'scriptHead'是異步的。因此,嘗試在'scriptHead.onload = function(){var script = w.document.createElement(「SCRIPT」)中設置以下邏輯: /*...*/};' –