gpt4 book ai didi

javascript - 为 document.createElement 'src' 设置 ('script' 属性?

转载 作者:行者123 更新时间:2023-11-28 15:05:57 25 4
gpt4 key购买 nike

我正在尝试在不同的选项卡上创建 Highcharts 。但我收到错误, Highcharts 未定义。我尝试使用不同的方法注册 Highchart.js。代码如下。

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();
}

最佳答案

您需要等待上一个脚本加载。 Demo .

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";

// bind on script load event
scriptHead.onload = function() {
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();
}

// as A.Wolff mentioned you might need to set onload befor src for some browsers.
scriptHead.src = link;
w.document.head.appendChild(scriptHead);
}

关于javascript - 为 document.createElement 'src' 设置 ('script' 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38940594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com